[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Instance Variable Names Available from Class?

Chris Gardner

2/26/2009 4:44:00 PM

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

5 Answers

lasitha

2/26/2009 5:25:00 PM

0

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). =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
--> [:@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/to...

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

Cheers,
lasitha

Brian Candler

2/26/2009 7:50:00 PM

0

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

Chris Gardner

2/26/2009 8:00:00 PM

0

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

Gary Wright

2/26/2009 8:28:00 PM

0


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.


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


Rick DeNatale

2/27/2009 1:53:00 AM

0

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

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
> 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.denh...articles/2008/02/08/whose-variable-is...

--
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...