Trying to add a very rudimentary description template to one of my Rails models. I want to make a template string as follows:
template = "{{ name }} is the best {{ occupation }} in {{ city }}."
and the hash like this:
vals = {:name => "Joe Smith", :occupation => "birthday clown", :city => "Las Vegas"}
and get the generated description. I thought I could do this with a simple gsub, but Ruby 1.8.7 does not accept hashes as the second argument. When I make gsub as a block like this:
> template.gsub(/\{\{\s*(\w+)\s*\}\}/) {|m| vals[m]} => " is the best in ."
You can see that it replaces it with the whole string (with curly braces), and not a match.
How can I replace it with "{{something}}" vals ["something"] (or vals ["something" .to_sym])?
TIA
Callmeed
source share