From: Satoshi Nakajima Date: 2007-10-18T09:28:23+09:00 Subject: Re: nil.to_i returning zero ------=_Part_36103_13149338.1192667304579 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline nil is an interesting beast. In one of my projects, I added "each" to NilClass class NilClass def each end end so that I can treat nil returned from IO:select as if it is an empty array. I like this change personally, and it does make sense to me logically ("doing something on each element of nothing" is equivalent to "do nothing"), but does it make sense to everybody? What if somebody fail to capture the bug because of this change? The same question applies to "nil+1". Does it make sense to everybody? Somebody may say "if nil+1 is 1, then, 1+nil should be 1", then somebody else may say "how about nil+nil"? This may eventually become very philosophical discussion, where there is no single answer... Satoshi 2007/10/17, Dirk Traulsen : > > Am 17 Oct 2007 um 21:08 hat Robert Dober geschrieben: > > > On 10/17/07, Dirk Traulsen wrote: > > > > > I think this is a reasonable proposal: > > > > > > class NilClass > > > def +(obj) > > > obj > > > end > > > end > > > > > > Can you think of any case in which this poses a problem? > > > > Hmm, let me see > > a = [42] > > b += a > > b << [43] ouch that hurts > > > > maybe > > class NilClass > > def + obj > > obj.clone rescue obj > > end > > end > > You're right. That's really better. > (You didn't have to look too hard for a problem, did you? :) Sometimes > I'm blind! ) > > But in earnest, I really think this would be a good thing to have in > Ruby. > > I'm not so sure about the other cases *,- and /. There you would have > to make a not so short list of object classes for which they must be > handled differently. > > But not for +, there it is very clear: > nothing + x is always x, independant from the nature of x. > > So, as this is universally correct, it could go in Ruby itself. > > > ------=_Part_36103_13149338.1192667304579--