ruby eval('\1') of gsub possible?
Posted
by Horace Ho
on Stack Overflow
See other posts from Stack Overflow
or by Horace Ho
Published on 2010-03-29T07:59:12Z
Indexed on
2010/03/29
8:03 UTC
Read the original article
Hit count: 497
I try to replace a sub-str by the content of a valiable where its name matches the sub-str by:
>> str = "Hello **name**"
=> "Hello **name**"
>> name = "John"
=> "John"
str.gsub(/\*\*(.*)\*\*/, eval('\1')) # => error!
the last line in the code above is a syntax error. and:
>> str.gsub(/\*\*(.*)\*\*/, '\1')
=> "Hello name"
>> str.gsub(/\*\*(.*)\*\*/, eval("name"))
=> "Hello John"
what I want is the result of:
str.gsub(/\*\*(.*)\*\*/, eval("name")) # => "Hello John"
any help will be appreciated. thx!
© Stack Overflow or respective owner