"made in Japan"
John Mettraux at openwfe.org
Performs 'dollar substitution' on a piece of text with a given dictionary.
require 'rubygems'
require 'rufus/dollar'
h = {
"name" => "Fred Brooke",
"title" => "Silver Bullet"
}
puts Rufus::dsub "${name} wrote '${title}'", h
# => "Fred Brooke wrote 'Silver Bullet'"
# File lib/rufus/dollar.rb, line 49 def self.dsub (text, dict, offset=nil) text = text.to_s j = text.index('}', offset || 0) return text unless j t = text[0, j] i = t.rindex('${') ii = t.rindex("\\${") iii = t.rindex('{') iii = nil if offset return text unless i return dsub(text, dict, j+1) if (iii) and (iii-1 > i) return unescape(text) if (i) and (i != 0) and (ii == i-1) # # found "\${" key = text[i+2..j-1] value = dict[key] value = if value value.to_s else dict.has_key?(key) ? 'false' : '' end pre = (i > 0) ? text[0..i-1] : '' dsub("#{pre}#{value}#{text[j+1..-1]}", dict) end
Generated with the Darkfish Rdoc Generator 2.