From: Jeremy McAnally Date: 2007-10-15T12:31:26+09:00 Subject: Re: nil.to_i returning zero I expect to get a converted object or some *Error (TypeError maybe?) back from any to_* method. Giving me the same object back or nil doesn't make any sense... As others have noted, I think the current behavior makes sense: I have nil (whatever...money, cats, etc.) I'd think an integer representation would be 0. --Jeremy On 10/14/07, Gerardo Santana Gómez Garrido wrote: > zero in Ruby is true, not false, in a boolean context. > > What does false.to_i return? An exception. There is not a numeric > interpretation for false. > > What about nil. nil is nothing. The only other object that evaluates > to false in a boolean context. > > What does nil.to_i return? Zero. And I wonder why. How can nil be > interpreted as a number. It's beyond me. > > Anyone care to explain it to me? > > So far I've found the following links about the issue: > > http://www.oreillynet.com/ruby/blog/2005/12/is_this_a_good_thing_or_not.html > http://journal.dedasys.com/articles/2007/05/08/rails-bitten-by-rubys-nil-to_f > http://groups.google.com/group/ruby-talk-google/browse_thread/thread/a475bb8a541f700e/b9f0cac6945a4210?lnk=gst&q=nil.to_i#b9f0cac6945a4210 > > -- > Gerardo Santana > > -- http://www.jeremymcanally.com/ My books: Ruby in Practice http://www.manning.com/mcanally/ My free Ruby e-book http://www.humblelittlerubybook.com/ My blogs: http://www.mrneighborly.com/ http://www.rubyinpractice.com/