>> string = '#{var}'
=> "\#{var}"
>> proc = Proc.new { |var| string }
=> #<Proc:0xb717a8c4@(pry):6>
>> proc.call(123)
=> "\#{var}"
Not really what I want. Double quotes around string result in the obvious undefined local variable.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Although this is possible, it’s not going to work how you intend here without having to use
eval, and generally that’s a bad idea if there’s an alternative. The good news is you have several options.The most straightforward is to use
sprintfformatting which is made even easier with theString#%method:This is a really reliable method as anything that supports the
.to_smethod will work and won’t cause the universe to implode if it contains executable code.