-2

I have a Ruby narray automatically generated, which has this form: [x,y], where x and y are integers.

I want to transform [x,y] into this type of string:

"p\.x\.y"

I haven't succeeded in transforming it using a regular expression.

4

2 回答 2

2

正则表达式用于匹配模式,而不是生成字符串。

要完成您想要的,只需使用 splat 运算符sprintf

array = [1,2]
puts sprintf("p\\.%d\\.%d", *array)

这将输出"p\.1\.2"

于 2013-10-27T16:47:53.397 回答
0

替代方式:

x = [1,2]
puts "p\\.#{x[0]}\\.#{x[1]}" #=> p\.1\2
于 2013-10-27T19:43:43.393 回答