From: dblack@... Date: 2003-01-26T14:46:43+09:00 Subject: Re: Reflection for instance variables? Hi -- On Sun, 26 Jan 2003, Patrick Narkinsky wrote: > Hi. I'm brand new to Ruby, so please pardon if this question is naive, Welcome to Ruby! > but I have not been able to figure it out. I would like to come up with > a way to define a method in a superclass that would give me a list of > variables in a subclass. So: > > class Father > @h_test = "1234" > @h_test2 = "12345" > > def Father.vars > return self.instance_vars # This does not work. I think you mean "instance_variables". > end > end > > class Son < Father > @h_test3 = "123456" > end > > print Son::vars > > ## end listing > > Would give me ['h_test', 'h_test2', 'h_test3']. It seems to me that > the key would be to get a reference to the Class object for the > subclass, but can't seem to figure out how to do that. Suggestions? I'm not 100% sure what you mean, but let me try something which probably isn't what you mean, and maybe you can bridge the gap or bounce it back: class Father def self.report_ivs ancestors.map {|a| a.instance_variables}.flatten.compact end @x = 1 @y = 2 end class Son < Father @z = 3 end p Father.report_ivs # ["@y", "@x"] p Son.report_ivs # ["@z", "@y", "@x"] David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav