Instance Variable Names Available from Class?

C

Chris Gardner

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?
 
L

lasitha

In metaprogramming, I know how to get the instance variable names from
an Object (through instance_variables). =A0Is there a way to get them fro= m
a Class?

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 =3D 'bar'
04> end
05> def baz=3D(arg)
06> @baz =3D arg
07> end
08> end
--> nil
09> Foo.new.instance_variables
--> [:mad: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:
http://www.ruby-forum.com/topic/107601

Hope that gives you some new perspectives (and eventually, the answer
to your original question!)

Cheers,
lasitha
 
B

Brian Candler

Chris said:
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?
 
C

Chris Gardner

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 said:
Chris said:
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?
 
G

Gary Wright

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.


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
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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.

http://talklikeaduck.denhaven2.com/articles/2008/02/08/whose-variable-is-it-anyway

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

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top