TIL: Ruby string substitution with hashes
gsub values from a hash
So this one was a surprise to me today, filed in the shoulda-known folder.
Straight from the rdoc:
h = {'foo' => 'bar', 'baz' => 'bat'}
'food'.sub('foo', h)
# => "bard"
It doesn't work if the hash keys are symbols though.
Member discussion