Ruby String.chomp Remix
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
About this entry
You’re currently reading “Ruby String.chomp Remix,” an entry on Connecting the dots
- Published:
- 2.25.08 / 4pm
- Category:
- Programming
- Tags:
- ruby, ruby chomp, ruby squeeze, ruby strip
No comments
Jump to comment form | comments rss [?] | trackback uri [?]