# (no subject)

**URL:** <https://www.ruby-forum.com/t/no-subject/79371>\
**Category:** Ruby\
**Created:** [December 14, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/no-subject/79371 "2006-12-14T16:52:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maksim\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Maksim\_B](https://www.ruby-forum.com/u/Maksim_B)\
**Post date:** [December 14, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/no-subject/79371/1 "2006-12-14T16:52:04Z")

</div>

Hi!

I’m quite new to ruby language. Can anyone please explain why in the  
attached ruby code the setter methods return their argument instead of  
the  
last statement’s value, and the string ‘weird stuff’ gets output only  
once?

I’ve been using ruby 1.8.4 (2005-12-24) to run this script.

Thanks ahead.

---

<div class="post-metadata">

**Author:** ![Maksim\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Maksim\_B](https://www.ruby-forum.com/u/Maksim_B)\
**Post date:** [December 14, 2006, 5:02pm UTC](https://www.ruby-forum.com/t/no-subject/79371/2 "2006-12-14T17:02:06Z")

</div>

On 12/14/06, Maksim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi!
> 
> I’m quite new to ruby language. Can anyone please explain why in the  
> attached ruby code the setter methods return their argument instead of the  
> last statement’s value, and the string ‘weird stuff’ gets output only once?

I think you attached the wrong Ruby code. It’s probably better to just  
post  
the code in your email (unless it’s really large).

---

<div class="post-metadata">

**Author:** ![Maksim\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Maksim\_B](https://www.ruby-forum.com/u/Maksim_B)\
**Post date:** [December 14, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/no-subject/79371/3 "2006-12-14T17:20:12Z")

</div>

Whoops. Sorry about that.

Here’s the actual code:

class Parent

def dummy  
“dummy”  
end

def dummy=(arg)  
puts ‘weird stuff’  
“dummy=”  
end

def set\_dummy(arg)  
“set\_dummy”  
end

end

class Child \< Parent

def try\_dummy1  
puts(dummy)  
end

def try\_dummy2  
puts(dummy = true)  
end

def try\_dummy3  
puts(set\_dummy(true))  
end

end

c = Child.new  
puts “#”  
c.try\_dummy1  
puts “#”  
puts(c.dummy)  
puts “#”  
c.try\_dummy2  
puts “#”  
puts(c.dummy = true)  
puts “#”  
c.try\_dummy3  
puts “#”  
puts(c.set\_dummy(true))  
class Parent

def dummy  
“dummy”  
end

def dummy=(arg)  
puts ‘weird stuff’  
“dummy=”  
end

def set\_dummy(arg)  
“set\_dummy”  
end

end

class Child \< Parent

def try\_dummy1  
puts(dummy)  
end

def try\_dummy2  
puts(dummy = true)  
end

def try\_dummy3  
puts(set\_dummy(true))  
end

end

c = Child.new  
puts “#”  
c.try\_dummy1  
puts “#”  
puts(c.dummy)  
puts “#”  
c.try\_dummy2  
puts “#”  
puts(c.dummy = true)  
puts “#”  
c.try\_dummy3  
puts “#”  
puts(c.set\_dummy(true))

---

<div class="post-metadata">

**Author:** ![Maksim\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Maksim\_B](https://www.ruby-forum.com/u/Maksim_B)\
**Post date:** [December 14, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/no-subject/79371/4 "2006-12-14T17:44:18Z")

</div>

Hi –

On Fri, 15 Dec 2006, Maksim B. wrote:

> On 12/14/06, pat eyler [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On 12/14/06, Maksim B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > Hi!
> > > 
> > > I’m quite new to ruby language. Can anyone please explain why in the  
> > > attached ruby code the setter methods return their argument  
> > > instead of the last statement’s value, and the string ‘weird  
> > > stuff’ gets output only once?

Methods ending with equal signs return the rhs, rather than their last  
value, so that they’ll be more assignment-like. That way, this:

x = 1

and this:

obj.thing = 1

have very close to the same sematics.

As for ‘weird stuff’ getting printed only once: it’s because you’ve  
got dummy = true (a local variable assignment) instead of self.dummy =  
true (a call to self.dummy=) in try\_dummy2.

David

---

<div class="post-metadata">

**Author:** ![Maksim\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Maksim\_B](https://www.ruby-forum.com/u/Maksim_B)\
**Post date:** [December 14, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/no-subject/79371/5 "2006-12-14T17:54:21Z")

</div>

Thank you for your reply.

Does this mean that the interpreter uses a different strategy to select  
a  
method depending on whether the message recipient is explicitly  
specified,  
or not? Can you explain in a bit more detail, please?

---

<div class="post-metadata">

**Author:** ![Maksim\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Maksim\_B](https://www.ruby-forum.com/u/Maksim_B)\
**Post date:** [December 14, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/no-subject/79371/6 "2006-12-14T17:56:48Z")

</div>

Maksim,

I am not sure what you are trying to accomplish with the code below but  
I will go through line by line and let you know what each method will  
return to try to give you a better picture,

Maksim B. wrote:

> Here’s the actual code:
> 
> class Parent
> 
> def dummy  
> “dummy”  
> end

This method simply returns a string of “dummy”

> def dummy=(arg)  
> puts ‘weird stuff’  
> “dummy=”  
> end

This method entitled “dummy=” will NOT assign anything to anything and  
although it looks like a mutator, it isn’t. It will simply print ‘weird  
stuff’ and return a string: dummy=

> def set\_dummy(arg)  
> “set\_dummy”  
> end

Once again, this method appears to be affecting state, but it isn’t. It  
simply returns a string: “set\_dummy”

> class Child \< Parent
> 
> def try\_dummy1  
> puts(dummy)  
> end

Calling this will print out: dummy as it will call Parent.dummy() which  
returns a string: dummy

> def try\_dummy2  
> puts(dummy = true)  
> end

This will call out Parent.dummy=() which prints out: weird stuff and  
then returns the string: dummy= which is then printed out by this  
method. It’s important to note, that once again no assignment is taking  
place here whatsoever, Is that what you are confused on?

> def try\_dummy3  
> puts(set\_dummy(true))  
> end

This method will call out Parent.set\_dummy() which returns a string:  
set\_dummy which this method then prints out…

I hope this has clarified some of your questions. I believe you are  
misusing string quotation marks but once again I am unsure of what you  
are trying to accomplish.

ilan

---

<div class="post-metadata">

**Author:** ![Maksim\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Maksim\_B](https://www.ruby-forum.com/u/Maksim_B)\
**Post date:** [December 14, 2006, 6:00pm UTC](https://www.ruby-forum.com/t/no-subject/79371/7 "2006-12-14T18:00:30Z")

</div>

Ilan B. wrote:

> This method simply returns a string of “dummy”
> 
> > def dummy=(arg)  
> > puts ‘weird stuff’  
> > “dummy=”  
> > end
> 
> This method entitled “dummy=” will NOT assign anything to anything and  
> although it looks like a mutator, it isn’t. It will simply print ‘weird  
> stuff’ and return a string: dummy=

Whoops! my mistake, as Dave mentioned, this method will return the RHS  
and not “dummy=”. This was explicitly mentioned in his book and I now  
hang my head in shame… sorry for the confusion…

ilan

---

<div class="post-metadata">

**Author:** ![Maksim\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6de8d8/32.png) [@Maksim\_B](https://www.ruby-forum.com/u/Maksim_B)\
**Post date:** [December 14, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/no-subject/79371/8 "2006-12-14T18:08:22Z")

</div>

Hi –

On Fri, 15 Dec 2006, Maksim B. wrote:

> > > > >
> > 
> > and this:
> 
> Does this mean that the interpreter uses a different strategy to select a  
> method depending on whether the message recipient is explicitly specified,  
> or not? Can you explain in a bit more detail, please?

Yes. When it sees this:

x = 1

(bareword = value)

it always reads that as a local variable assignment. Therefore, =  
methods always have to have an explicit receiver.

David
