1 min read

TIL: Ruby string substitution with hashes

gsub values from a hash
TIL: Ruby string substitution with hashes
Photo by Brett Jordan / Unsplash

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.