# Class setter

**URL:** <https://www.ruby-forum.com/t/class-setter/154246>\
**Category:** Ruby\
**Created:** [November 26, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/class-setter/154246 "2008-11-26T17:47:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vespasian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vespasian](https://www.ruby-forum.com/u/vespasian)\
**Post date:** [November 26, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/class-setter/154246/1 "2008-11-26T17:47:54Z")

</div>

For the life of me I can figure out why the setter below returns “v” and  
not nil

class A  
def initialize(id)  
@id = id  
end

def id  
@id  
end

def id=(v)  
nil  
end  
end

a = A.new(1)  
a.id = 2  
=\> 2

Any idea?  
TIA,  
ves

---

<div class="post-metadata">

**Author:** ![vespasian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vespasian](https://www.ruby-forum.com/u/vespasian)\
**Post date:** [November 26, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/class-setter/154246/2 "2008-11-26T17:53:30Z")

</div>

On Nov 26, 10:43 am, Ves P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> def id=(v)  
> nil  
> end  
> end
> 
> a = A.new(1)  
> a.id = 2  
> =\> 2

That’s just the way setters work in Ruby. Any method ending in = will  
return what you pass to it.  
That way, it’s analogous to setting regular variables, like x = y = z.

On a similar note, though you “configure” the class method .new with  
the instance method #initialize, you can’t make .new return something  
other than an instance of the class just by the return value of  
#initialize.

---

<div class="post-metadata">

**Author:** ![vespasian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vespasian](https://www.ruby-forum.com/u/vespasian)\
**Post date:** [November 26, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/class-setter/154246/3 "2008-11-26T18:04:42Z")

</div>

Yossef M. wrote:

> On a similar note, though you “configure” the class method .new with  
> the instance method #initialize, you can’t make .new return something  
> other than an instance of the class just by the return value of  
> #initialize.

That’s not really similar. The only connection between new and  
initialize is  
that new calls initialize. There is no rule that a method that calls  
another  
method should return the return value of that method (given that new  
calls  
two methods, that’d be impossible anyway). So new returning the newly  
created  
object even though it calls initialize is perfectly ordinary. The  
behaviour  
of setters, however, is a special case.

---

<div class="post-metadata">

**Author:** ![vespasian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/f07891/32.png) [@vespasian](https://www.ruby-forum.com/u/vespasian)\
**Post date:** [November 26, 2008, 6:09pm UTC](https://www.ruby-forum.com/t/class-setter/154246/4 "2008-11-26T18:09:25Z")

</div>

On Nov 26, 10:59 am, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> That’s not really similar. The only connection between new and initialize is  
> that new calls initialize. There is no rule that a method that calls another  
> method should return the return value of that method (given that new calls  
> two methods, that’d be impossible anyway). So new returning the newly created  
> object even though it calls initialize is perfectly ordinary. The behaviour  
> of setters, however, is a special case.

Good point and clarification. It just seems similar to me because  
normal usage involves defining #initialize and calling .new, but it’s  
as you say – one method calls the other and doesn’t use that return  
value.
