# Beginning meta-programming question

**URL:** <https://www.ruby-forum.com/t/beginning-meta-programming-question/192198>\
**Category:** Ruby\
**Created:** [July 26, 2010, 6:48am UTC](https://www.ruby-forum.com/t/beginning-meta-programming-question/192198 "2010-07-26T06:48:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [July 26, 2010, 6:48am UTC](https://www.ruby-forum.com/t/beginning-meta-programming-question/192198/1 "2010-07-26T06:48:35Z")

</div>

I’m pretty new to ruby, and just playing around. I can’t work out why  
the  
following won’t work, though:

1. module TrackInheritors
2. def self.extended(parent)
3. 

```
 class << parent

```

4. 

```
   puts self

```

5. 

```
   @@inheritors = []

```

6. 
7. 

```
   def self.inheritors

```

8. 

```
     @@inheritors

```

9. 

```
   end

```

10. 
11. 

```
   def self.inherited(child)

```

12. 

```
     @@inheritors << child

```

13. 

```
   end

```

14. 

```
 end

```

15. end
16. end
17. 
18. class Parent
19. extend TrackInheritors
20. end
21. 
22. class Child1 \< Parent
23. end
24. 
25. class Child2 \< Parent
26. end
27. 
28. puts Parent.inheritors
29. 
30. 
# -:28: undefined method `inheritors’ for Parent:Class

(NoMethodError)

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [July 27, 2010, 1:29am UTC](https://www.ruby-forum.com/t/beginning-meta-programming-question/192198/2 "2010-07-27T01:29:41Z")

</div>

I’m a noob myself so sorry if I give incorrect information but I think  
you have a going out of scope issue. The inheirtors method is defined  
within the class that starts on line 3. However, the class it’s put in  
is inside the extended method on line 2, so what I think is going on is  
that by the time you’re trying to the inheirtors method, its class has  
already gone out of scope since it’s called after the extended method  
has ended.

---

<div class="post-metadata">

**Author:** ![rawroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@rawroe](https://www.ruby-forum.com/u/rawroe)\
**Post date:** [July 26, 2010, 8:34am UTC](https://www.ruby-forum.com/t/beginning-meta-programming-question/192198/3 "2010-07-26T08:34:31Z")

</div>

On Mon, Jul 26, 2010 at 6:48 AM, Andrew W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 1. 
> 
> ```
> @@inheritors
> 
> ```
> 
> 2. extend TrackInheritors
> 3. 
> # -:28: undefined method `inheritors’ for Parent:Class
> 
> (NoMethodError)

When you open the singleton class of parent, you don’t need to define  
the methods for “self.”, just define them directly, cause you are  
already on the singleton class:

module TrackInheritors  
def self.extended(parent)  
class \<\< parent  
puts self  
@@inheritors = []

```
  def inheritors
    @@inheritors
  end

  def inherited(child)
    @@inheritors << child
  end
end

```

end  
end

class Parent  
extend TrackInheritors  
end

class Child1 \< Parent  
end

class Child2 \< Parent  
end

puts Parent.inheritors

$ ruby inheritors.rb  
#Class:Parent  
Child1  
Child2

I’m not really sure where were you defining those methods, in the  
singleton class of the singleton class?

Jesus.
