Good example of antiquity of some Core Ruby classes

by john on September 22, 2009

A common pattern in Ruby is to call .to_s on an object when you want a String, even if the object itself might be a String. So, e.g., if you are getting NoMethodErrors on nils when you need a String, you might call .to_s to convert that nil into a String (”).

Well, you might want to do the same thing to convert Floats to BigDecimals. But guess what? BigDecimal doesn’t include a .to_d method! Oops. The BigDecimal class must be so old that this idiom hadn’t evolved.

Leave a Comment

Previous post: Ruby inherited callback runs before subclass is loaded

Next post: If you must rescue Exception . . .