"Simon Strandgaard" <neoneye / adslhome.dk> schrieb im Newsbeitrag
news:pan.2003.11.28.20.00.51.850573 / adslhome.dk...
> On Sat, 29 Nov 2003 04:47:37 +0900, Hal Fulton wrote:
>
> > Simon Strandgaard wrote:
> >> a = Iterator.new(42)
> >> a += 1   # Question:  How to do selfassignment ?
> >> a.close
> >> # 42 never gets closed!
> >
> > What's happening here is that += is dependent on +, yet it is still
> > an assignment.
> >
> > a += 1 is equivalent to a=a+1; so as you can see, a new object is
> > created and assigned to a.
>
> I know that. What I don't know, are if there are any tricks when dealing
> with '+=' ?
>
>
> > You could always implement methods like succ or add that would
> > change the value without changing the object's identity.
>
> Yes I already have #next and #prev.
>
>
> > Does this clarify any?
>
> Conclusion: providing a PLUS ('+') operator, can be dangerous because
the
> user can invoke '+=' and the instance will not get #closed correctly.
> safer to undef '+'.

IMHO "+" carries the wrong semantics for an iterator in Ruby: you don't
want a new object to be created, instead you want the iterator to change
its state.

If you want to provide numeric updates, why not just define succ like
this:

def succ(inc=1) ... end

Regards

    robert