accessing class instance variable from instance method

Discussion in 'Ruby' started by David Garamond, Jun 8, 2004.

  1. Is there an easier way other than:

    class C
    @var = "class instance"
    def C.var; @var end
    def class_instance_var; @var = "instance"; self.class.var; end
    end

    p C.new.class_instance_var # -> class_instance

    --
    dave
    David Garamond, Jun 8, 2004
    #1
    1. Advertising

  2. "David Garamond" <lists@zara.6.isreserved.com> schrieb im Newsbeitrag
    news:40C57D4C.5020603@zara.6.isreserved.com...
    > Is there an easier way other than:


    To achieve what?

    > class C
    > @var = "class instance"
    > def C.var; @var end
    > def class_instance_var; @var = "instance"; self.class.var; end
    > end
    >
    > p C.new.class_instance_var # -> class_instance


    Regards

    robert
    Robert Klemme, Jun 8, 2004
    #2
    1. Advertising

  3. Robert Klemme wrote:
    > "David Garamond" <lists@zara.6.isreserved.com> schrieb im Newsbeitrag
    > news:40C57D4C.5020603@zara.6.isreserved.com...
    >
    >>Is there an easier way other than:

    >
    > To achieve what?


    Sorry. See subject. (To access class instance variable from inside
    instance method).

    My very small rant is, accessing class variables (@@var) from inside
    instance method is much easier and more straightforward; however, I tend
    to use class instance variable more often, since in Ruby1.x @@var is
    shared in subclasses (IIRC this will change in Ruby2). I've never find a
    use for @@var so far in my programs.

    >> class C
    >> @var = "class instance"
    >> def C.var; @var end
    >> def class_instance_var; @var = "instance"; self.class.var; end
    >> end
    >>
    >> p C.new.class_instance_var # -> class_instance


    --
    dave
    David Garamond, Jun 8, 2004
    #3
  4. "David Garamond" <lists@zara.6.isreserved.com> schrieb im Newsbeitrag
    news:40C5815E.5000503@zara.6.isreserved.com...
    > Robert Klemme wrote:
    > > "David Garamond" <lists@zara.6.isreserved.com> schrieb im Newsbeitrag
    > > news:40C57D4C.5020603@zara.6.isreserved.com...
    > >
    > >>Is there an easier way other than:

    > >
    > > To achieve what?

    >
    > Sorry. See subject. (To access class instance variable from inside
    > instance method).


    No, my fault. I should have read a bit more carefully. :)

    > My very small rant is, accessing class variables (@@var) from inside
    > instance method is much easier and more straightforward; however, I tend
    > to use class instance variable more often, since in Ruby1.x @@var is
    > shared in subclasses (IIRC this will change in Ruby2). I've never find a
    > use for @@var so far in my programs.


    Mee, too.

    IMHO you did it appropriately. Some remarks though:

    - I just don't get why you do @var = "instance" in method
    'class_instance_var'.

    - I would not define an instance method that accesses the class's
    attribute other than using it as an obfuscating technic. The class has
    already an accessor for that. Implementing a method like you did makes
    the user think it's an instance var.

    - You might want to consider synchronizing access to the class var if
    you're in a multithreaded env, because multiple threads might be accessing
    the class concurrently.

    - I prefer "def self.var; @var; end" because that is safe with respect to
    class renaming.

    > >> class C
    > >> @var = "class instance"
    > >> def C.var; @var end
    > >> def class_instance_var; @var = "instance"; self.class.var; end
    > >> end
    > >>
    > >> p C.new.class_instance_var # -> class_instance


    Kind regards

    robert
    Robert Klemme, Jun 8, 2004
    #4
  5. Robert Klemme wrote:
    > - I just don't get why you do @var = "instance" in method
    > 'class_instance_var'.


    That's just for testing :) (to make sure the right '@var' is printed).

    > - I would not define an instance method that accesses the class's
    > attribute other than using it as an obfuscating technic. The class has
    > already an accessor for that. Implementing a method like you did makes
    > the user think it's an instance var.


    I seldom do this too. Only when I want to expose a class' attribute to
    class instance users (and usually only read only too).

    > - You might want to consider synchronizing access to the class var if
    > you're in a multithreaded env, because multiple threads might be accessing
    > the class concurrently.


    I see. Thanks.

    > - I prefer "def self.var; @var; end" because that is safe with respect to
    > class renaming.


    Yes. Old habits (due to examples in Pickaxe) :)

    --
    dave
    David Garamond, Jun 8, 2004
    #5
  6. David Garamond

    Ara.T.Howard Guest

    On Tue, 8 Jun 2004, David Garamond wrote:

    > Is there an easier way other than:
    >
    > class C
    > @var = "class instance"
    > def C.var; @var end
    > def class_instance_var; @var = "instance"; self.class.var; end
    > end
    >
    > p C.new.class_instance_var # -> class_instance
    >
    > --
    > dave


    i've been setting up all my classes like this:


    class C

    class << self
    def init
    @foo = 4
    @bar = 2
    end
    end
    self.init


    attr :foo

    def initialize arg = nil
    @foo = arg || klass.foo
    end

    def klass
    self.class
    end

    end

    same thing - bit more convenient.

    -a
    --
    ===============================================================================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | A flower falls, even though we love it; and a weed grows, even though we do
    | not love it. --Dogen
    ===============================================================================
    Ara.T.Howard, Jun 8, 2004
    #6
    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. marekw2143
    Replies:
    3
    Views:
    1,351
    marekw2143
    Jul 25, 2009
  2. Raj Singh
    Replies:
    2
    Views:
    189
    Rick DeNatale
    May 29, 2008
  3. Greg Hauptmann
    Replies:
    9
    Views:
    235
    Loren Segal
    Jun 16, 2008
  4. Subashini Kumar
    Replies:
    5
    Views:
    127
    Ilan Berci
    Sep 23, 2009
  5. Chuck Remes
    Replies:
    3
    Views:
    173
    hemant
    Aug 12, 2010
Loading...

Share This Page