Sunday, February 20, 2011

Stripping dollar signs and commas from a string


It is moments like these that you realise why it is you love Ruby so much…
Today was one of those moments with the String class’s :delete method.
I have used this method many, many times in the past.. and this is nothing new for me… but it was one of those moments where I just went “Gee, I love coding in Ruby…” and I wanted to share :)
If you ever need to convert, say $12,345.12 into a single float number like12345.12 a Ruby newbie, might do something like this:
"$12,345.12".gsub("$",'').gsub(",",'') #=> "12345.12"
But hark! Look into the string class and you shall find the :deletemethod, which lets you do this:
"$12,345.12".delete("$,") #=> "12345.12"

No comments:

Post a Comment