(think)

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

Using Ruby's Gsub With a Hash

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:

1
2
3
4
5
6
def geekify(string)
  string.gsub(/[leto]/, 'l' => '1', 'e' => '3', 't' => '7', 'o' => '0')
end

geekify('leet') # => '1337'
geekify('noob') # => 'n00b'

Keep in mind you’re not restricted to single character replacements:

1
2
3
4
5
6
def doctorize(string)
  string.gsub(/M(iste)?r/, 'Mister' => 'Doctor', 'Mr' => 'Dr')
end

doctorize('Mister Freeze') # => 'Doctor Freeze'
doctorize('Mr Smith')   # => 'Dr Smith'

That’s all for today folks! I hope you’ll find this short article useful!

Comments