Class setter

Discussion in 'Ruby' started by Ves Pasian, Nov 26, 2008.

  1. Ves Pasian

    Ves Pasian Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Ves Pasian, Nov 26, 2008
    #1
    1. Advertising

  2. On Nov 26, 10:43=A0am, Ves Pasian <> wrote:
    > For the life of me I can figure out why the setter below returns "v" and
    > not nil
    >
    > class A
    > =A0 def initialize(id)
    > =A0 =A0 @id =3D id
    > =A0 end
    >
    > =A0 def id
    > =A0 =A0 @id
    > =A0 end
    >
    > =A0 =A0def id=3D(v)
    > =A0 =A0 =A0nil
    > =A0 =A0end
    > end
    >
    > a =3D A.new(1)
    > a.id =3D 2
    > =3D> 2


    That's just the way setters work in Ruby. Any method ending in =3D will
    return what you pass to it.
    That way, it's analogous to setting regular variables, like x =3D y =3D 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.

    --
    -yossef
     
    Yossef Mendelssohn, Nov 26, 2008
    #2
    1. Advertising

  3. Yossef Mendelssohn 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.

    --
    NP: Sentenced - My Slowing Heart
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Nov 26, 2008
    #3
  4. On Nov 26, 10:59=A0am, Sebastian Hungerecker <>
    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 anot=

    her
    > method should return the return value of that method (given that new call=

    s
    > two methods, that'd be impossible anyway). So new returning the newly cre=

    ated
    > object even though it calls initialize is perfectly ordinary. The behavio=

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

    --
    -yossef
     
    Yossef Mendelssohn, Nov 26, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VHUtVGhhY2g=?=

    DataBinder setter method.

    =?Utf-8?B?VHUtVGhhY2g=?=, Mar 15, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    346
    =?Utf-8?B?VHUtVGhhY2g=?=
    Mar 15, 2005
  2. Raoul Markus
    Replies:
    3
    Views:
    1,104
    Roedy Green
    Sep 20, 2003
  3. Timo Nentwig
    Replies:
    75
    Views:
    3,350
    Jon A. Cruz
    Jan 18, 2004
  4. kaeli
    Replies:
    5
    Views:
    16,019
    Sudsy
    Jan 5, 2004
  5. Stacey
    Replies:
    1
    Views:
    1,892
    Collin VanDyck
    Feb 10, 2004
Loading...

Share This Page