From: "Simon Kröger" Date: 2005-08-09T06:55:53+09:00 Subject: Re: polymorphism and/or named parameters: the ruby way? Joel VanderWerf wrote: > Simon Kr�ger wrote: > >>>>This very useful little method is a nice thing to have in your >>>>bag-o-tricks. >>> >>> >>>Nice. you could even call it #then: >>> >>>class Object >>> def then >>> yield(self) >>> self >>> end >>>end >>> >>>#Then you could do these types of things: >>>a = (0..10).to_a >>>i = 3 >>>p a[i.then{i+=1}] >>>p i >>> >>>[..snip..] >> >> >>But be carefull: >> >>i = 3 >>p i.then{i+=1} >>p i >> >>i = [3] >>p i.then{i << 4} >>p i >> >> >>output: >>3 >>4 >>[3, 4] >>[3, 4] >> >> >>(perhaps this was obvious to all, except me) >> >>cheers >> >>Simon > > > Good point. You really have to be especially clear about whether you are > modifying a variable binding or an object. > > i = [3] > p i.then{i+=[4]} # ==> [3] > p i # ==> [3, 4] > woohoo, i+=[4] creates a new array, right?! this is - lets say - 'suboptimal' in terms of speed for the 99.9% of cases where you do not need the old array. (ok, you should realy use <<, except if you use 'then' - I would not like to teach someone this) cheers Simon