Return to Snippet

Revision: 8813
at October 9, 2008 15:21 by dbug13


Updated Code
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

Revision: 8812
at October 9, 2008 15:20 by dbug13


Initial Code
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

Initial URL

                                

Initial Description

                                

Initial Title
replace fields in a string with data

Initial Tags
textmate, ruby

Initial Language
Ruby