attr_* position in a class and their results

Discussion in 'Ruby' started by Arno J., Aug 14, 2007.

  1. Arno J.

    Arno J. Guest

    Hello,
    I was playing with instance variables when I made up those two classes :

    class Vartest_before
    def init
    @instance_variable = "I belong to the instance"
    end

    attr_accessor :instance_variable

    def instance_variable
    @instance_variable = "Forced"
    end
    end

    class Vartest_after
    def init
    @instance_variable = "I belong to the instance"
    end

    def instance_variable
    @instance_variable = "Forced"
    end

    attr_accessor :instance_variable
    end

    Now when using them, here are the results (# =>) :

    a = Vartest_after.new
    a.init
    b = Vartest_before.new
    b.init

    puts a.instance_variable # => I belong to the instance
    a.instance_variable = "Changed"
    puts a.instance_variable # => Changed

    puts b.instance_variable # => Forced
    b.instance_variable = "Changed"
    puts b.instance_variable # => Forced


    I don't understand what's happening :/ Can you ?
    --
    Posted via http://www.ruby-forum.com/.
    Arno J., Aug 14, 2007
    #1
    1. Advertising

  2. Alle marted=C3=AC 14 agosto 2007, Arno J. ha scritto:
    > Hello,
    > I was playing with instance variables when I made up those two classes :
    >
    > class Vartest_before
    > def init
    > @instance_variable =3D "I belong to the instance"
    > end
    >
    > attr_accessor :instance_variable
    >
    > def instance_variable
    > @instance_variable =3D "Forced"
    > end
    > end
    >
    > class Vartest_after
    > def init
    > @instance_variable =3D "I belong to the instance"
    > end
    >
    > def instance_variable
    > @instance_variable =3D "Forced"
    > end
    >
    > attr_accessor :instance_variable
    > end
    >
    > Now when using them, here are the results (# =3D>) :
    >
    > a =3D Vartest_after.new
    > a.init
    > b =3D Vartest_before.new
    > b.init
    >
    > puts a.instance_variable # =3D> I belong to the instance
    > a.instance_variable =3D "Changed"
    > puts a.instance_variable # =3D> Changed
    >
    > puts b.instance_variable # =3D> Forced
    > b.instance_variable =3D "Changed"
    > puts b.instance_variable # =3D> Forced
    >
    >
    > I don't understand what's happening :/ Can you ?



    In the case of Vartest_before, when you call=20

    puts b.instance_variable

    the value of @instance_variable is changed to 'Forced' before being returne=
    d.=20
    If you substitute that line with

    puts b.instance_variable_get:)@instance_variable)

    you'd get what (I think) you expect, that is 'Changed'. This doesn't happen=
    =20
    for the other class because there your definition of instance_variable is=20
    overwritten by the one provided by attr_accessor.

    I hope this helps

    Stefano
    Stefano Crocco, Aug 14, 2007
    #2
    1. Advertising

  3. Arno J. wrote:
    > class Vartest_before
    > def init
    > @instance_variable = "I belong to the instance"
    > end
    >
    > attr_accessor :instance_variable


    This defines a method instance_variable

    > def instance_variable
    > @instance_variable = "Forced"
    > end
    > end


    This defines the method instance_variable again. The version created by
    attr_accessor doesn't exist anymore.

    > class Vartest_after
    > def init
    > @instance_variable = "I belong to the instance"
    > end
    >
    > def instance_variable
    > @instance_variable = "Forced"
    > end
    >
    > attr_accessor :instance_variable
    > end


    This time it's the other way around: First you define the method with
    def, then you redefine it with attr_accessor. It's like doing
    def bla()
    1
    end
    def bla()
    2
    end
    Everytime you call bla, it will return 2 because the second definition
    overrides the first one.


    HTH,
    Sebastian
    --
    NP: Metallica - Trapped Under Ice
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Aug 14, 2007
    #3
  4. Arno J.

    Arno J. Guest

    Ok, I'm must be tired because I was expecting just the opposite, which
    wasn't logical.
    Sorry for this dumb question.
    --
    Posted via http://www.ruby-forum.com/.
    Arno J., Aug 14, 2007
    #4
  5. On 14.08.2007 21:34, Arno J. wrote:
    > Ok, I'm must be tired because I was expecting just the opposite, which
    > wasn't logical.


    Maybe it was even caused by using a different order in class definition
    and usage. This got me confused initially as well. :)

    > Sorry for this dumb question.


    No prob.

    Kind regards

    robert
    Robert Klemme, Aug 14, 2007
    #5
    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. David A. Black
    Replies:
    4
    Views:
    121
    David A. Black
    Jul 12, 2005
  2. Patrick Gundlach

    eval, attr_* like methods

    Patrick Gundlach, Aug 12, 2005, in forum: Ruby
    Replies:
    10
    Views:
    205
    Patrick Gundlach
    Aug 13, 2005
  3. David Brady

    Writing my own attr_* methods

    David Brady, Nov 9, 2005, in forum: Ruby
    Replies:
    4
    Views:
    132
    Trans
    Nov 10, 2005
  4. aidy
    Replies:
    2
    Views:
    89
  5. Pedro Silva

    attr_*, *variables_*

    Pedro Silva, Sep 6, 2008, in forum: Ruby
    Replies:
    4
    Views:
    83
    David A. Black
    Sep 6, 2008
Loading...

Share This Page