"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