Posted By

dbug13 on 10/09/08


Tagged

textmate ruby


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


replace fields in a string with data


 / Published in: Ruby
 

  1. info = "Hello World %firstname% %lastname% today is %date% ."
  2. fields = { 'firstname' => "Jamie", 'lastname' => "Allison"}
  3.  
  4. info.gsub!(/(%.*?%)/) do |m|
  5. m.gsub!(/%/, '')
  6. fields[m]
  7. end
  8.  
  9. puts info
  10. # >> Hello World Jamie Allison today is .
  11. # Note if field is not in fields array, it is simply replaced with an empty
  12. # string, like the %date% field above

Report this snippet  

You need to login to post a comment.