Stripping dollar signs and commas from a string

Fri May 14 02:07:33 -0700 2010

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 like 12345.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 :delete method, which lets you do this:

"$12,345.12".delete("$,") #=> "12345.12"

Ahhh… pure Ruby bliss.

blogLater

Mikel

  1. Sharad Says:

    If you are needing this for ActiveRecord attributes, you should also look at attribute-normalizer: http://github.com/mdeering/attribute_normalizer

  2. uorenshom Says:

    Your website is very useful for me , i like it very much, i get many informations from this !

  3. valuation model Says:

    I am sending it to a few friends ans also sharing in delicious. Thanks for making such a killer blog. I arrive on here all the time and am floored with the fresh information here!

  4. oil press machine Says:

    Your website is very useful for me , i like it very much, i get many informations from this !

  5. yacht charter italy Says:

    it is the first time when I used this sign in my projects. And seems to be easy to do it.

  6. project management tools Says:

    Wonderful web site. Plenty of helpful information here. I am sending it to a few friends ans also sharing in delicious.

  7. How to Get Scholarships Says:

    Your website is very useful for me ,How to Get Scholarships i like it very much, i get many informations from this !

  8. Movers in Virginia beach Says:

    Hmm, I used to use Microsoft Excel to coding and writing like this. Currency, date, etc. I can do by using this application. I think the results are quite accurate and very easy to use.

  9. cosmetic dentist houston Says:

    it is a great resources. I will follow your tips.

  10. Quirky gifts distributor Says:

    This is really a great post. I have been wasting my time among different blogs, but thanks to writer who has solved my problem here.

  11. Sheffield Solicitors Says:

    I wanted to send you a very small remark to finally say thank you the moment again considering the magnificent views you’ve shared on this page.

  12. Diabetes Says:

    Good Work ! I think the results are quite accurate and very easy to use. Thanks for sharing!

  13. bestowadeel Says:

    This is really a nice blog. I have read many blogs but this one really satisfies your real needs. I just love to bookmark this page. Really was not expecting that when I started off studying.Security in Slough | Security in St Albans | Security Companies London | Security in Crawley | Security in St Davids | Car breakers in Birmingham

  14. Cool Clock Says:

    I find a really good blog, which is informative and also interesting. I really enjoyed reading through your blog.

  15. Joseph Joseph Says:

    I never believed that surfing the web can be very beneficial and have fun in a good shape. I feel really happy and grateful for giving me such valuable post.

  16. Notary Public Kensington Says:

    Commenting on this site is a pleasure for me. I had really liked reading this site it was very good. The content of this blog was very nice.

  17. timeline for facebook mandatory Says:

    I understand how to do it, but where can we apply it? I am not sure that I understand!

Leave a Reply