From: mark sparshatt Date: 2005-05-06T23:00:18+09:00 Subject: Re: nil.to_? and ruby2.0 Gergely Kontra wrote: > Hi! > > To the nil war: > > Then what is the reason nil.to_i and friends exists? > > Will it hide bugs? > > I'm using 1.8, there nil.to_i exists,but > a=nil > b=2 > > b+a # nil cannot be coerced into Fixnum > > So where to use nil.to_i? > you'd use nil.to_i in a case like this a = some_method 2 + a.to_i if you knew that some_method could return nil and that treating it as zero was the right thing to do. I.E. if you've made a concious desicion to treat nil as zero However if we allowed this a = some_method 2 + a #=> then whether or not it's reasonable for some_method to return a nil, it would still be silently handled whether you want it to or not. So to_i and friends are there so a developer can use them to hide nils if the program warrants it, but this hiding isn't the default behaviour, so unexpected nils don't get hidden. Which seems a reasonable compromise between flexibility and error detection. -- Mark Sparshatt