(unknown)

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.

test.rb (204 Bytes)

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

···

On 12/14/06, Maksim Bartenev <bartenev@gmail.com> wrote:

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

Thanks ahead.

--
thanks,
-pate
-------------------------

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

···

On 12/14/06, pat eyler <pat.eyler@gmail.com> wrote:

On 12/14/06, Maksim Bartenev <bartenev@gmail.com> 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).

Hi --

> 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

···

On Fri, 15 Dec 2006, Maksim Bartenev wrote:

On 12/14/06, pat eyler <pat.eyler@gmail.com> wrote:

On 12/14/06, Maksim Bartenev <bartenev@gmail.com> wrote:

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

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

···

--
Posted via http://www.ruby-forum.com/\.

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?

···

On 12/14/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Fri, 15 Dec 2006, Maksim Bartenev wrote:
>
> On 12/14/06, pat eyler <pat.eyler@gmail.com> wrote:
>>
>> On 12/14/06, Maksim Bartenev <bartenev@gmail.com> 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

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Ilan Berci 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

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Fri, 15 Dec 2006, Maksim Bartenev wrote:

On 12/14/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Fri, 15 Dec 2006, Maksim Bartenev wrote:
>
> On 12/14/06, pat eyler <pat.eyler@gmail.com> wrote:
>>
>> On 12/14/06, Maksim Bartenev <bartenev@gmail.com> 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.

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?

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

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)