(no subject)

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.

On 12/14/06, Maksim B. [email protected] 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).

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))

Hi –

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

On 12/14/06, pat eyler [email protected] wrote:

On 12/14/06, Maksim B. [email protected] 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

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?

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

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

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