ruby - Interpolate string from database -
for example:
t = test.new t.test_string = "\#{foo} , \#{bar}" t.save and want interpolate string in 1 of method.
i tried several options:
erb.newfoo = 'hello' bar = 'world' erb.new(t.test_string).result
it not work, test_string print "\#{foo} , \#{bar}".
it work if print without escape symbol '\'.
erb.new("#{foo} , #{bar}").result => "hello , world" but how can make programatic?
evalfoo = 'hello' bar = 'world' eval '"' + t.test_string + '"'
it works not safe.
do have other options? or how make erb.new work?
maybe don't quite understand needs.
looking for?
require 'erb' erb_template = erb.new('foo equals <%= bar %> , 2 + 2 = <%= 2 + 2 %>') bar = 'baz' erb_template.result(binding) # => foo equals baz , 2 + 2 = 4 binding method captures current scope erb rendering template in scope.
Comments
Post a Comment