# Class\_extension and module\_extension

**URL:** <https://www.ruby-forum.com/t/class-extension-and-module-extension/99303>\
**Category:** Ruby\
**Created:** [July 21, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/class-extension-and-module-extension/99303 "2006-07-21T16:39:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 21, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/class-extension-and-module-extension/99303/1 "2006-07-21T16:39:48Z")

</div>

Just a quick comment about class\_extension. In using I’ve come across  
cases where I want the extension to propogate through further  
inclusions and cases where I do not. So I think we new two versions,  
one for each. Perhapos the propogating version caould be called  
module\_extension.

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 21, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/class-extension-and-module-extension/99303/2 "2006-07-21T19:00:49Z")

</div>

This is actually pretty interesting. I’m in the midst of deep meta code  
and I have the situation where I’m creating a sort of DSL for building  
classes --you know, defining my own attr methods. And then I use that  
in a module which is to be included into my class. That way I keep  
these DSL methods out of my class (qua class). Eg.

module MetaDSL  
def foos() @foos ||=[] end  
def metafoo x  
foos \<\< x  
attr\_reader “#{x}oo”  
end  
end

module MetaFoo  
extend MetaDSL  
metafoo :f  
end

class Foo  
include MetaFoo  
end

The problem is that I actaully want #foos to show up in my class (qua  
class) but not metafoo! How’s can I achieve that?

In the future there’s class\_extension, and I suggested  
module\_extiension too, one for propogating and one not (which is  
which?) So a solution would be:

module MetaDSL  
# lets say class\_extension propogates  
class\_extension do  
def foos() @foos ||=[] end  
end  
# and module\_extension does not  
module\_extension do  
def metafoo x  
foos \<\< x  
attr\_reader “#{x}oo”  
end  
end  
end

module MetaFoo  
include MetaDSL  
metafoo :f  
end

But uh oh! Now the two methods can’t see each other. Well, maybe we can  
fix by including the returned module of each when invoked without a  
block:

module MetaDSL  
class\_extension do  
def foos() @foos ||=[] end  
end  
include class\_extension

```
module_extension do
  def metafoo x
     foos << x
     attr_reader "#{x}oo"
  end
end
include module_extension

```

end

Will that work? Well, I assume somthing to that effect would. But even  
so, now I’m looking at this and thinking it got quite complex, when all  
I really want to do is _control visibility_, much like public, private  
and protected. But local, single\_provision, and propogated (for lack of  
better terms).

T.
