Hi. I’m brand new to Ruby, so please pardon if this question is naive,
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.
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?
(For the curious, I’m trying to do something in Ruby I saw in
objective-c. This class allowed you to define a subclass, define the
variables in the subclass, then it would create the sql, etc.
automagically.)
Thanks,
Patrick
Hi. I’m brand new to Ruby, so please pardon if this question is naive,
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:
dont think a class method is going to cut it, for a couple of reasons.
but perhaps this:
class Father
@h_test = “1234”
@h_test2 = “12345”
def vars
return self.instance_vars - self.superclass.instance_vars
end
end
class Son < Father
@h_test3 = “123456”
end
s = Son.new
print s.vars
NOT TESTED!!!
(For the curious, I’m trying to do something in Ruby I saw in
objective-c. This class allowed you to define a subclass, define the
variables in the subclass, then it would create the sql, etc.
automagically.)
sounds fun! and good luck.
···
On Saturday 25 January 2003 08:34 pm, Patrick Narkinsky wrote:
–
tom sawyer, aka transami
transami@transami.net
Hi –
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
···
On Sun, 26 Jan 2003, Patrick Narkinsky wrote:
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
thanks - that’s exactly what I was looking for. In retrospect, I
suppose it was naive to expect Ruby to story the instance variables for
all inherited classes in one place.
Patrick
···
On Sunday, January 26, 2003, at 12:46 AM, dblack@candle.superlink.net wrote:
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: