While i was sitting down playing with regular expressions, i decided to write a small application String  chomp method my way. If perhaps you wanted to remove all whitespaces within and around a string you would have to do it using strip and squeeze methods but the same effect can be achieved by using regular expressions. It even works with n or t etc.

First, we scan for words with w+ and join them with a single space.

1  #Building my own chomp

2  class String
3    def bchomp
4      self.scan(/w+/).join(‘ ‘)
5    end
6  end