Snippet: Multi-Search and Replace

Posted by kev Mon, 16 Apr 2007 19:52:00 GMT

template = <<-EOT
%HEAD%
%SUBHEAD%

%BODY%

%FOOT%
EOT

replacements = {/%HEAD%/ => "A title", /%SUBHEAD%/ => "A subhead",
                /%BODY%/ => "Something about ruby perhaps", 
                /%FOOT%/ => "Creative Commons: BY-NC-SA"}

replacements.each do |search, replace|
  template.gsub! search, replace
end

puts template 
# =>
# A title
# A subhead
# 
# Something about ruby perhaps
# 
# Creative Commons: BY-NC-SA

Posted in  | no comments

Comments

Comments are disabled