# Method behaviour

**URL:** <https://www.ruby-forum.com/t/method-behaviour/101627>\
**Category:** Ruby\
**Created:** [August 6, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/method-behaviour/101627 "2006-08-06T22:38:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 6, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/method-behaviour/101627/1 "2006-08-06T22:38:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 6, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/method-behaviour/101627/2 "2006-08-06T22:47:25Z")

</div>

On 06/08/06, Sard A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> alter the original object outside of the method, where as others don’t.  
> 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/](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’).

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 6, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/method-behaviour/101627/3 "2006-08-06T22:50:25Z")

</div>

Hi –

On Mon, 7 Aug 2006, Sard A. wrote:

> alter the original object outside of the method, where as others don’t.  
> 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?

It’s because there’s a distinction between (a) sending a message to an  
object, and (b) assigning to a variable.

a = “cow” # assignment  
a[0] = ‘w’ # send message “[]” to a  
a = “fish” # assignment – reusing the identifier ‘a’

Whenever you assign to a variable, you’re wiping the slate clean: the  
variable loses all association with the object it referred to  
previously (if any).

The “word[0] = ‘e’” expression looks like assignment, but technically  
it’s calling the method “[]=” on the object word, with the single  
argument ‘e’. It’s not the same as just plain assignment to an  
identifier.

David

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 7, 2006, 2:20am UTC](https://www.ruby-forum.com/t/method-behaviour/101627/4 "2006-08-07T02:20:42Z")

</div>

Your assessment is correct but you got one detail wrong. The message  
sent by

```
  word[0]='e'

```

is actually []= with arguments 0 and ‘e’. That is, the pseudo-  
assignment is really the message

```
  word.[]=(0, 'e')

```

As a coating of syntactic sugar, Ruby permits us to write this as  
something that looks like an assignment and which, consequently, can  
confuse people like the OP.

Regards, Morton

P.S. Don’t think I’m complaining about the syntactic sugar: the real  
message underlying the sugar coating is ugly. I don’t want to write  
stuff like that.

---

<div class="post-metadata">

**Author:** ![sard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@sard](https://www.ruby-forum.com/u/sard)\
**Post date:** [August 7, 2006, 3:20am UTC](https://www.ruby-forum.com/t/method-behaviour/101627/5 "2006-08-07T03:20:44Z")

</div>

Hi –

On Mon, 7 Aug 2006, Morton G. wrote:

> Your assessment is correct but you got one detail wrong. The message sent by
> 
> word[0]=‘e’
> 
> is actually []= with arguments 0 and ‘e’.

Whoops, right: two arguments.

David
