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:

  1. erb.new

    foo = '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?

  1. eval

    foo = '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

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -