Inheritance problem with dynamic methods

S

Stefano Grioni

Hello,

I'm tying to create a method which would allow me to get and set
instance variables having the same name as the class. And this should be
true even for classes inheriting it.

For instance

Code:
class Mother
define_method(self.to_s.downcase){ instance_variable_get
'@'<<self.class.to_s.downcase }
define_method((self.to_s.downcase+'=').to_sym){|nom|
instance_variable_set '@'<<self.class.to_s.downcase, nom}
end
class Daughter < Mother
end

At the moment, this code creates "mother" and "mother=" instance methods
for both Mother and Daughter's classes.
Instead, what I'd like to have in a "mother" and "mother=" instance
methods for Mother, and "daughter" and "daughter=" for Daughter.

I think that there is some meta programming trick that I'm missing but I
can't find which one.

Could you help me?
 
D

David A. Black

Hi --

Hello,

I'm tying to create a method which would allow me to get and set
instance variables having the same name as the class. And this should be
true even for classes inheriting it.

For instance

Code:
class Mother
define_method(self.to_s.downcase){ instance_variable_get
'@'<<self.class.to_s.downcase }
define_method((self.to_s.downcase+'=').to_sym){|nom|
instance_variable_set '@'<<self.class.to_s.downcase, nom}
end
class Daughter < Mother
end

At the moment, this code creates "mother" and "mother=" instance methods
for both Mother and Daughter's classes.
Instead, what I'd like to have in a "mother" and "mother=" instance
methods for Mother, and "daughter" and "daughter=" for Daughter.

I think that there is some meta programming trick that I'm missing but I
can't find which one.

One metaprogramming trick you could use is attr_accessor :) Another
is the "inherited" hook. Try this:

class Mother
def self.attr_eponymous(c=self)
c.send:)attr_accessor, c.to_s.downcase)
end

def self.inherited(c)
attr_eponymous(c)
end

attr_eponymous
end


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)
 
S

Stefano Grioni

David said:
Hi --



One metaprogramming trick you could use is attr_accessor :) Another
is the "inherited" hook. Try this:

class Mother
def self.attr_eponymous(c=self)
c.send:)attr_accessor, c.to_s.downcase)
end

def self.inherited(c)
attr_eponymous(c)
end

attr_eponymous
end


David

Hi,

Your solution is brilliant :D
I must admit that I was so sure that the solution would come of some
weird twist of a singleton method that I completely skipped the
"inherited" hook ...

Thanks a lot!
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top