# Inheritance problem with dynamic methods

**URL:** <https://www.ruby-forum.com/t/inheritance-problem-with-dynamic-methods/172425>\
**Category:** Ruby\
**Created:** [July 21, 2009, 12:51pm UTC](https://www.ruby-forum.com/t/inheritance-problem-with-dynamic-methods/172425 "2009-07-21T12:51:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [July 21, 2009, 12:51pm UTC](https://www.ruby-forum.com/t/inheritance-problem-with-dynamic-methods/172425/1 "2009-07-21T12:51:59Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [July 21, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/inheritance-problem-with-dynamic-methods/172425/2 "2009-07-21T14:19:59Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Tue, 21 Jul 2009, Stefano G. wrote:
> 
> > ```
> > define_method(self.to_s.downcase){ instance_variable_get
> > 
> > ```
> > 
> > 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©  
> attr\_eponymous©  
> end
> 
> attr\_eponymous  
> end
> 
> David

Hi,

Your solution is brilliant 😃  
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!

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [July 21, 2009, 1:59pm UTC](https://www.ruby-forum.com/t/inheritance-problem-with-dynamic-methods/172425/3 "2009-07-21T13:59:15Z")

</div>

Hi –

On Tue, 21 Jul 2009, Stefano G. wrote:

> ```
> define_method(self.to_s.downcase){ instance_variable_get
> 
> ```
> 
> 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©  
attr\_eponymous©  
end

attr\_eponymous  
end

David
