An online novel about the Source, the Force, the real life and everything in between...

Using Ruby's Gsub With a Hash

Oct 3rd, 2013

Recently we discussed how you can use String#gsub with a block.
Today we’ll examine another somewhat unknown feature of the gsub method – the ability to supply a replacement hash as the second argument (which is normally a string).

If the replacement argument is a hash, and the matched text is one of its keys, the corresponding value is the replacement string. Here’s a simple example: