In metaprogramming, I know how to get the instance variable names from
an Object (through instance_variables). Is there a way to get them from
a Class?
···
--
Posted via http://www.ruby-forum.com/.
In metaprogramming, I know how to get the instance variable names from
an Object (through instance_variables). Is there a way to get them from
a Class?
--
Posted via http://www.ruby-forum.com/.
Hello Chris, before answering your question, let me ask: does
#instance_variables really work the way you think it does?
Consider:
$: irb
01> class Foo
02> def initialize
03> @bar = 'bar'
04> end
05> def baz=(arg)
06> @baz = arg
07> end
08> end
--> nil
09> Foo.new.instance_variables
--> [:@bar]
Why isn't @baz listed?
What would you have to do before it would be listed?
Ponder on that, then read over this previous thread:
Hope that gives you some new perspectives (and eventually, the answer
to your original question!)
Cheers,
lasitha
On Thu, Feb 26, 2009 at 10:13 PM, Chris Gardner <chris.r.gardner@gmail.com> wrote:
In metaprogramming, I know how to get the instance variable names from
an Object (through instance_variables). Is there a way to get them from
a Class?
Chris Gardner wrote:
In metaprogramming, I know how to get the instance variable names from
an Object (through instance_variables). Is there a way to get them from
a Class?
A class *is* an object, with its own instance variables, and you can see
them in the same way:
class Foo
@bar = 123
def self.flurble
@bar
end
end
p Foo.instance_variables # ["@bar"]
p Foo.flurble # 123
Or did you mean something else?
--
Posted via http://www.ruby-forum.com/\.
What I mean is that I would like to ask a class for the names of
instance variables of its objects, which may have yet to be
instantiated.
class Foo
def intialize(bar, baz)
@bar = bar
@baz = baz
end
end
I was trying to ask class Foo for the names @bar and @baz. I've found
out that these are accessible only through Foo instances.
I'm a Ruby newbie and am just today learning about Ruby's approach to
metaprogramming.
Brian Candler wrote:
Chris Gardner wrote:
In metaprogramming, I know how to get the instance variable names from
an Object (through instance_variables). Is there a way to get them from
a Class?A class *is* an object, with its own instance variables, and you can see
them in the same way:class Foo
@bar = 123def self.flurble
@bar
end
endp Foo.instance_variables # ["@bar"]
p Foo.flurble # 123
Or did you mean something else?
--
Posted via http://www.ruby-forum.com/\.
The question doesn't apply in Ruby since instance variables are dynamically created on a per object basis. Even if you use standard helpers such as attr_accessor and company, those methods simply create instance methods that manipulate instance variables--they don't actually create the instance variables.
Just to be clear, there is no language-supported way to introspect a Ruby class and determine what instance variables may or may not be supported by all instances of an object. You can't even introspect to find out what methods were created by the attr* family of methods and what methods were simply defined explicitly. Sure you could come up with heuristics such as:
If there is a method called x and another called x= then there is probably an instance variable named @x.
But that is just a guess, not something supported by the language semantics.
The idea that each instance of a class can have its own behavior and state that is independent of its class definition and that can evolve over time is something that must be understood before a programmer can fully grasp Ruby's programming and object model.
Gary Wright
On Feb 26, 2009, at 2:59 PM, Chris Gardner wrote:
What I mean is that I would like to ask a class for the names of
instance variables of its objects, which may have yet to be
instantiated.
http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-variable-is-it-anyway
On Thu, Feb 26, 2009 at 2:59 PM, Chris Gardner <chris.r.gardner@gmail.com>wrote:
What I mean is that I would like to ask a class for the names of
instance variables of its objects, which may have yet to be
instantiated.class Foo
def intialize(bar, baz)
@bar = bar
@baz = baz
end
endI was trying to ask class Foo for the names @bar and @baz. I've found
out that these are accessible only through Foo instances.I'm a Ruby newbie and am just today learning about Ruby's approach to
metaprogramming.
--
Rick DeNatale
Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale