0

我正在使用厨师。为什么这不起作用?

someString[aSearchString]= `pwd`

对我来说,aSearchString最终不会被替换。我必须pwd在单独的行中执行到变量中,然后使用它来进行替换。

4

1 回答 1

2

尝试这个:

some_string.gsub('search_string', `pwd`)

>> s="./asd"   #=> "./asd"
>> s.gsub('.', `pwd`) #=> "/home/rahul\n/asd"
>> s.gsub('.', `pwd`.chop) #=> "/home/rahul/asd"   #chop because 'pwd` adds \n at end
>> s.gsub!('.', `pwd`.chop)  #=> "/home/rahul/asd" #inplace

基准:

ree-1.8.7-2011.12 :005 > my_bm(1000){"/./././.".gsub(".", `pwd`)}
   user     system      total        real
0.040000   0.760000   0.800000 (  2.690505)
=> nil 
ree-1.8.7-2011.12 :006 > my_bm(1000){c=`pwd`;"/./././.".gsub(".", c)}
   user     system      total        real
0.140000   0.660000   0.800000 (  2.692811)
=> nil 
ree-1.8.7-2011.12 :007 > my_bm(1000){"/./././.".gsub(".", `pwd`)}
   user     system      total        real
0.090000   0.720000   0.810000 (  2.705673)
=> nil 
ree-1.8.7-2011.12 :008 > my_bm(1000){c=`pwd`;"/./././.".gsub(".", c)}
   user     system      total        real
0.090000   0.720000   0.810000 (  2.688737)
=> nil 
于 2013-01-30T19:05:01.090 回答