Forum: Ruby Instance Variable Names Available from Class?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2bff0ba03264e51c94027c5a9b609306?d=identicon&s=25 Chris Gardner (gardnercr)
on 2009-02-26 17:44
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?
E16e84e861c1815ce11ba7bd851c857d?d=identicon&s=25 lasitha (Guest)
on 2009-02-26 18:28
(Received via mailing list)
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?

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:
http://www.ruby-forum.com/topic/107601

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

Cheers,
lasitha
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-02-26 20:51
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?
2bff0ba03264e51c94027c5a9b609306?d=identicon&s=25 Chris Gardner (gardnercr)
on 2009-02-26 21:00
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?
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2009-02-26 21:30
(Received via mailing list)
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
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-02-27 02:55
(Received via mailing list)
On Thu, Feb 26, 2009 at 2:59 PM, Chris Gardner
<chris.r.gardner@gmail.com>wrote:

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

--
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
This topic is locked and can not be replied to.