在 Ruby 1.8.6(2007-09-24 补丁级别 111)中:
str = '\&123'
puts "abc".gsub("b", str) => ab123c
puts "abc".gsub("b", "#{str}") => ab123c
puts "abc".gsub("b", str.to_s) => ab123c
puts "abc".gsub("b", '\&123') => ab123c
puts "abc".gsub("b", "\&123") => a&123c <--- This I want to achieve using temporary variable
如果我str = '\&123'
改为str = "\&123"
它工作正常,但我str
从match
函数中得到,所以我不能在括号内手动指定它。有没有办法改变'string'
to"string"
行为?