On 10/15/07, hadley wickham <h.wickham / gmail.com> wrote:
> On 10/15/07, Rick DeNatale <rick.denatale / gmail.com> wrote:

> >
> > class NilClass
> >
> >    def +(aNumber)
> >        aNumber
> >    end
> >
> >    def -(aNumber)
> >       0 - aNumber
> >    end
> >
> >    alias_method :*, :to_i
> >    alias_method: :/, :to_i
> > end
> >
> > which would allow sequences like:
> >
> >   a ||= 0
> >   a += 1
> >
> > To be just
> >   a += 1
>
> Why is addition so special?  What about multiplication and division?

Note that I did give implementations of NilClass#* and NilClass#/
which treat nil as 0. I just didn't give an example.

> Really, you probably want nil to be the idempotent element for any
> operation (ie. f(a, nil) = a).

Don't think so, at least for - and /, if you want to go in that
direction though I think that it should be:

nil + x => x
nil - x => -x
nil * x => x
nil / x => 1 / x

On the other hand, this does seem inconsistent with nil.to_i being 0.

> But can you implement that?  Probably
> not.

For the 4 arithmetic operators certainly.


-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/