schneik / us.ibm.com writes:
> p list.collect { |i| i.sub(/.*/, "1_#{$&}_1") }
You can't use the $& form in a string parameter to sub, because the
parameters are evaluated _before_ the body of the method is
executed. You have to use \& instead
def x(list)
p list
p list.collect { |i| i.sub(/(.*)/, '1_\&_1') }
p list.collect { |i| i.sub(/.*/, '1_\&_1') } .
collect { |i| i.sub(/.*/, '2_\&_2') }
print "\n"
end
x( %w(a) )
x( %w(a b c d) )
=>
["a"]
["1_a_1"]
["2_1_a_1_2"]
["a", "b", "c", "d"]
["1_a_1", "1_b_1", "1_c_1", "1_d_1"]
["2_1_a_1_2", "2_1_b_1_2", "2_1_c_1_2", "2_1_d_1_2"]
Regards
Dave