Quickie: Ruby's string modulo method (%)

Posted by theBlatherskite Sun, 11 May 2008 20:52:00 GMT

Just came across this in Ruby today:


>> tmpl = "I am a stored %s with %d replacements"
=> "I am a stored %s with %d replacements"
>> tmpl % ['template', 2]
=> "I am a stored template with 2 replacements"
>> tmpl % ['fish', 17]
=> "I am a stored fish with 17 replacements"

Handy way to save formatted templates for printfing later, but a bit strange the first time you see the % method applied to a string.

Posted in ,  | Tags , , , ,  | no comments