[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Operator overloading

dblack

9/22/2003 10:49:00 AM

1 Answer

Carlos

9/22/2003 1:03:00 PM

0

> > > j += 1
> > >
> > > is expanded before evaluation to:
> > >
> > > j = j + 1
> >
> > Not quite - I think j += 1 only evaluates j once. I recall one instance
> > where that helped me, performance-wise.
>
> The j on the left isn''t being evaluated; it''s just being assigned to.
> So in cases with objects that can have more than one instance (like
> String), you get a new object:

He is referring to this case:

def counter
c=0
return proc { c+=1 }
end

cc = counter

a = [ "a", "b", "c" ]
a[cc.call] += "x" # only one cc.call evaluated, only a[1] changed
a # [ "a", "bx", "c" ]

cc = counter
a = [ "a", "b", "c" ]
a[cc.call] = a[cc.call] + "x" # a[1] = a[2] + "x"
a # [ "a", "cx", "c" ]