Accessing class instance variable from instance method

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

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

···

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

--

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

===============================================================================

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" <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. :slight_smile:

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 wrote:

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

That's just for testing :slight_smile: (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) :slight_smile:

···

--
dave