From: Pit Capitain Date: 2002-01-24T19:40:05+09:00 Subject: Re: Array.last Weirdness On 24 Jan 2002, at 19:28, Tobias Reif wrote: > Yukihiro Matsumoto wrote: > > > > a.[]=(-1,a[-1]+' bar') > > > > in other words > > > > a[-1] = a[-1] + ' bar' > > > Thank you very much. But I still don't understand :| > > While I know why the last two don't work (because there is no #last= > mehtod), I would expect all of the following to result in the same (or > I think it would be possible, and great, if they did): > > Without knowing "the internals", they all seem to say the same: > > = + > > a[-1] = a[-1] + ' bar' > > array.last = array.last + ' bar' > > or > array.last += " world" > > Tobi Hi Tobi, if you define the missing "last=" method using the existing "[]=" method, you get the desired behaviour: class Array def last=( value ) self[ -1 ] = value end end a = [ "works", "now:", "hello" ] a.last += " world" p a # => ["works", "now:", "hello world"] Regards, Pit