def myformat4(str, hash)
str.gsub( %r{%((\w+))([dsi])?} ) { “%#{$2}” % hash[$1] }
end
Note: I didn’t bother to look up all legal formatting characters.
This also doesn’t take care of complex formats, like “%-6.3f”. Could
easily be extended though, but since this requires common agreement (do
we use “%fmt(var)” or “%(var)fmt” ?), I’d much rather see Nobu’s RCR
come into being.
Also, I’m surprised no one offered the evil eval:
irb(main):001:0> s = ‘#{name}’
=> “#{name}”
irb(main):002:0> name = “Bob”
=> “Bob”
irb(main):003:0> eval “"#{s}"”
=> “Bob”
(Not that it deals with formats in anyway, or that I recommend using it