/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
info = "Hello World %firstname% %lastname% today is %date% ." fields = { 'firstname' => "Jamie", 'lastname' => "Allison"} info.gsub!(/(%.*?%)/) do |m| m.gsub!(/%/, '') fields[m] end puts info # >> Hello World Jamie Allison today is . # Note if field is not in fields array, it is simply replaced with an empty # string, like the %date% field above