From: Dave Thomas Date: 2002-01-24T14:11:50+09:00 Subject: Re: Array.last Weirdness Jesse Jones writes: > I'd expect the following code: > > array = ["hello"] > array.last += " world" > puts array > > to print "hello world", but instead I get errors like "undefined method > `last=' for ["hello"]:Array (NameError)". This doesn't seem too good... xxx += y is shorthand for xxx = xxx + y. When 'xxx' is the name of a variable, this makes sense. Take the value in the variable xxx, invoke its '+' method (passing in y), and then assign whatever comes back to 'xxx' However, array.last = array.last + y make less sense. What are you assigning the result to? There's no variable there, just the method call, array.last. Now Ruby's fairly clever here. It sees that you need an lvalue method, and so looks for a method called 'last='. When it can't find one, it gives up and reports the error. There's really nothing else it can do. However, rather than trying to assign to the string, you could use methods in class String to append to the element in the array: a = ["hello"] a.last.concat "wor" a.last << "ld" Regards Dave