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
Leave a Reply
Latest posts
- Encrypting Another Partition Using FileVault 2 on OSX Lion
- Installing Home Folder on Second Drive on OSX Lion
- undefined local variable or method `version_requirements'
- A New World of Resources
- Rails Static Pages
- Twitter Replacing Rails? So?
- Engine Yard Cloud Backups Generating Zero Length Backups
- Our Rails Rumble Entry - StillAlive.com
- Renaming a controller and redirection in Rails 3
- Updating RailsPlugins.org to Rails 3 - Part 1
- A new protocol for social interaction
- What is a distributed social network?
- Bundler and Public Git Sources
- Getting Heroku, memcached and Rails 3 working
- Why Bundler?
- Rails Commit Access
- Introducing TellThemWhen
- rake RSpec & Cucumber uninitialized constant Rails::Boot::Bundler
- This Relationship is Worth Nothing
- Thank YOU...
- Inline Attachments for ActionMailer
- Upgrading RailsPlugins.org to Rails 3 - Part 1
- Getting Rails 3 Edge with jQuery, RSpec and Cucumber using RVM
- Action Mailer, go Proc thyself
- The Real News Donation Drive
- ActionMailer ScreenCast and Article
- Installing RSpec for Rails 3
- I am speaking at RailsConf 2010
- Rails 3 Session Secret and Session Store
- If you're lazy and you know it write your specs!
- Bundler - uninitialized constant ActionController
- Bundle Me Some Sanity
- How to use Mail / ActionMailer 3 with GMail SMTP
- Put your mailer where the action is!
- Why Force a Choice?
- How to make an RSS feed in Rails
- Rails 3 Routing with Rack
- Bundle me some Rails
- Helping out in Haiti
- Watch your self
- Is Rails 3.0 a Game Changer?
- Where did the scripts go?
- validates :rails_3, :awesome => true
- New Rails Version 3.0 Guides Online
- New ActionMailer API in Rails 3.0
- Mail gem version 2 released
- How to rename a Rails 3 Application
- Rails 3.0 Examples
- DECCA Driving Day
Latest comments
- Quantspring.com
This is a really great post I f...
- Maxine
Maybe it has a connection with ...
- Axel W
Had the same problem lots of ti...
- cloud computing anbieter
I was there and I see you confe...
- cloud computing anbieter
I was there and I see you confe...
Categories
Tag Cloud
AJAX ARGH! ActiveRecord Ajax Apache Apple Asterisk Australia Copy Database Development Feedburner Gem server Google Human Rights Javascript L. Ron Hubbard MS SQL Server MacOSX Mail Mephisto Not Programming OpenBSD Opensource Performance Personal Integrity PostgreSQL Programming Prototype Puzzle RDoc REST RESTful Rails RSPec RSpec Rails Rails Tips Rspec Ruby Ruby on Rails Ruby on Rails Tips Ruby on rails Tips SQL SQLServer SVN Scientologist Scientology Site Stats Soekris Soekris net5501 TMail Textmate Tips Windows World about mikel anti drug apache contributing daemon documentation drugs illustrator javascript lambda mail mephisto newspapers nitro open source opensource photoshop productivity programming railscasts rspec ruby ruby on rails rubyforge scientology seo sitemap sqlserver tips tmail tom cruise unix tricks vector graphicsArchives
- November 2009 (1)
- October 2009 (2)
- September 2009 (2)
- August 2009 (0)
- July 2009 (1)
- June 2009 (0)
- May 2009 (1)
- April 2009 (0)
- March 2009 (0)
- February 2009 (0)
- January 2009 (2)
- December 2008 (0)
- November 2008 (5)
- October 2008 (0)
- September 2008 (1)
- August 2008 (0)
- July 2008 (2)
- June 2008 (13)
- May 2008 (7)
- April 2008 (18)
- March 2008 (8)
- February 2008 (5)
- January 2008 (7)
- December 2007 (20)
- November 2007 (22)




Tue May 18 15:19:38 -0700 2010
If you are needing this for ActiveRecord attributes, you should also look at attribute-normalizer: http://github.com/mdeering/attribute_normalizer
Tue Nov 08 19:42:10 -0800 2011
Your website is very useful for me , i like it very much, i get many informations from this !
Sat May 05 23:33:11 -0700 2012
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!
Mon Aug 08 02:45:49 -0700 2011
Your website is very useful for me , i like it very much, i get many informations from this !
Thu Feb 23 00:30:30 -0800 2012
it is the first time when I used this sign in my projects. And seems to be easy to do it.
Mon Feb 27 22:45:12 -0800 2012
Wonderful web site. Plenty of helpful information here. I am sending it to a few friends ans also sharing in delicious.
Wed Feb 29 20:34:02 -0800 2012
Your website is very useful for me ,How to Get Scholarships i like it very much, i get many informations from this !
Sat Mar 03 06:49:29 -0800 2012
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.
Sun Mar 04 03:47:52 -0800 2012
it is a great resources. I will follow your tips.
Wed Mar 07 23:17:36 -0800 2012
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.
Mon Mar 12 22:16:35 -0700 2012
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.
Tue Mar 20 05:39:25 -0700 2012
Good Work ! I think the results are quite accurate and very easy to use. Thanks for sharing!
Mon Mar 26 00:54:23 -0700 2012
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
Sat Apr 07 23:57:54 -0700 2012
I find a really good blog, which is informative and also interesting. I really enjoyed reading through your blog.
Wed Apr 11 02:13:01 -0700 2012
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.
Fri Apr 20 23:04:07 -0700 2012
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.
Fri Apr 27 07:05:23 -0700 2012
I understand how to do it, but where can we apply it? I am not sure that I understand!