David Garamond said:
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.
Kind regards
robert