From: Farrel Lifson Date: 2006-08-07T05:46:21+09:00 Subject: Re: Method behaviour On 06/08/06, Sard Aukary wrote: > Some methods like > > def change(word) > word[0]='e' > end > a="affluent" > puts a > change(a) > puts a > > alter the original object outside of the method, where as others don't. > > def change(word) > word="fish" > end > a="cow" > puts a > change(a) > puts a > > > Why the change in behaviour, what determines it? Is the only way I can > write methods that change the original object by using the Ruby methods > that do this? > > -- > Posted via http://www.ruby-forum.com/. > > In the second example you are changing the reference to where 'word' points. You create a new string ('fish') and then set 'word' to reference it. 'a' still points to your original word ('cow').