did you know gsub can be passed a block to operate on matches? I just learned it from this blog post.

I was looking for a way to backquote parentheses and spaces… here’s how I did it:

>> puts "this (is a) test".gsub( /[\s\(\)]/ ){ |c| "\\#{c}" }
this\ \(is\ a\)\ test
=> nil

Leave a Reply