[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

pat eyler

12/14/2006 4:00:00 PM

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

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


--
thanks,
-pate
-------------------------
http://on-ruby.bl...

4 Answers

dblack

12/14/2006 4:41:00 PM

0

Ilan Berci

12/14/2006 4:57:00 PM

0

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

Ilan Berci

12/14/2006 5:01:00 PM

0

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

dblack

12/14/2006 5:08:00 PM

0