# Result of assignment is not the return value

**URL:** <https://www.ruby-forum.com/t/result-of-assignment-is-not-the-return-value/185029>\
**Category:** Ruby\
**Created:** [March 12, 2010, 12:56am UTC](https://www.ruby-forum.com/t/result-of-assignment-is-not-the-return-value/185029 "2010-03-12T00:56:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [March 12, 2010, 12:56am UTC](https://www.ruby-forum.com/t/result-of-assignment-is-not-the-return-value/185029/1 "2010-03-12T00:56:07Z")

</div>

Given a simple class like this

class MyClass  
attr\_reader :value  
def value=(val)  
@value = val.to\_s  
end  
end

Why the the following code return the value passed and not the value  
assigned?

c = MyClass.new  
result = c.value = 2  
puts result  
puts c.value

This is currently outputing

2  
“2”

The results are the same when MyClass is modified to be this.

class MyClass  
attr\_reader :value  
def value=(val)  
@value = val.to\_s  
return @value  
end  
end

Shouldn’t the result of the ‘value=’ method be the return value?

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [March 12, 2010, 12:58am UTC](https://www.ruby-forum.com/t/result-of-assignment-is-not-the-return-value/185029/2 "2010-03-12T00:58:27Z")

</div>

Setters always return the value they were originally assigned

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [March 12, 2010, 4:39am UTC](https://www.ruby-forum.com/t/result-of-assignment-is-not-the-return-value/185029/3 "2010-03-12T04:39:44Z")

</div>

On Thu, Mar 11, 2010 at 6:03 PM, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â matz.

Ahh. Thanks. I figured it was something like that, but I couldn’t find  
any concrete reference to it.

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [March 12, 2010, 1:03am UTC](https://www.ruby-forum.com/t/result-of-assignment-is-not-the-return-value/185029/4 "2010-03-12T01:03:43Z")

</div>

Hi,

In message “Re: result of assignment is not the return value”  
on Fri, 12 Mar 2010 08:55:25 +0900, Nathan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|Shouldn’t the result of the ‘value=’ method be the return value?

It’s a design choice. We defined the value of the assignment as the  
value of the right hand expression, not the return value from the  
assigning method.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![ate50eggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/fbc32d/32.png) [@ate50eggs](https://www.ruby-forum.com/u/ate50eggs)\
**Post date:** [March 12, 2010, 8:30am UTC](https://www.ruby-forum.com/t/result-of-assignment-is-not-the-return-value/185029/5 "2010-03-12T08:30:54Z")

</div>

On Thu, Mar 11, 2010 at 9:39 PM, Nathan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> any concrete reference to it.

I have thought about this for awhile. One rationalization is this.

Think of “stacked assignment” which Ruby borrows from C.

x = y = 5

An accessor acts much like a simple assignment:

x = foo.bar = 5 # same as: x = (foo.bar = 5)

In this case, it would be weird if x ended up being anything  
other than 5.

Hal
