accessing class instance variable from instance method

D

David Garamond

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
 
R

Robert Klemme

David Garamond said:
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
 
D

David Garamond

Robert said:
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.
 
R

Robert Klemme

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
 
D

David Garamond

Robert said:
- 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) :)
 
A

Ara.T.Howard

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top