# Can't include a module in Enumerable?

**URL:** <https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595>\
**Category:** Ruby\
**Created:** [June 23, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595 "2007-06-23T20:49:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 23, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595/1 "2007-06-23T20:49:35Z")

</div>

irb(main):002:1\> module Z  
irb(main):003:1\> def x; “x”; end  
irb(main):004:1\> end  
=\> nil  
irb(main):005:0\> module Enumerable  
irb(main):006:1\> include Z  
irb(main):007:1\> end  
=\> Enumerable  
irb(main):008:0\> a = [1,2,3,4,5]  
=\> [1, 2, 3, 4, 5]  
irb(main):009:0\> a.x  
NoMethodError: undefined method `x’ for [1, 2, 3, 4, 5]:Array  
from (irb):9  
from :0

Is the Double Inclusion Problem worse than I realized? I thought the  
above would be okay b/c ‘a’ is instantiated _after_ the inclusion  
(which is why I’ve often called it the Dynamic Inclusion Problem).  
What gives?

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 23, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595/2 "2007-06-23T22:34:49Z")

</div>

On 6/23/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):009:0\> a.x  
> NoMethodError: undefined method `x’ for [1, 2, 3, 4, 5]:Array  
> from (irb):9  
> from :0
> 
> Is the Double Inclusion Problem worse than I realized? I thought the  
> above would be okay b/c ‘a’ is instantiated _after_ the inclusion  
> (which is why I’ve often called it the Dynamic Inclusion Problem).  
> What gives?

It’s not the instance, it’s the class Array, and that existed (and  
included Enumerable) before you changed Enumerable.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 23, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595/3 "2007-06-23T22:47:49Z")

</div>

On 6/23/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):009:0\> a.x  
> NoMethodError: undefined method `x’ for [1, 2, 3, 4, 5]:Array  
> from (irb):9  
> from :0
> 
> Is the Double Inclusion Problem worse than I realized? I thought the  
> above would be okay b/c ‘a’ is instantiated _after_ the inclusion  
> (which is why I’ve often called it the Dynamic Inclusion Problem).  
> What gives?

Rick answered this, but maybe this is a solution for what you’re trying  
to do:

> > module ZEnumerable  
> > include Enumerable  
> > def x; “x”; end  
> > end

> > class B  
> > include ZEnumerable

?\> def each

> > ```
> > [1,2,3].each { |e| yield(e) }
> > 
> > ```
> > 
> > end  
> > end

> > class Array  
> > include ZEnumerable  
> > end  
> > =\> Array  
> > [1,2,3].x  
> > =\> “x”  
> > B.new.map { |e| e + 1 }  
> > =\> [2, 3, 4]  
> > B.new.x  
> > =\> “x”

Yeah, you’d need to go remix existing enumerable classes, but it keeps  
you from having to do the additional include for _new_ objects

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 24, 2007, 10:38am UTC](https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595/4 "2007-06-24T10:38:35Z")

</div>

Tom, here’s something to experiment with. I haven’t tested it  
extensively:

class Module  
alias :old\_include :include  
def include other  
old\_include other  
if self.class == Module  
this = self  
ObjectSpace.each\_object Module do |mod|  
mod.module\_eval do include this end if mod \< self  
end  
end  
end  
end

module Z  
def x; “x”; end  
end

module Enumerable  
include Z  
end

a = [1,2,3,4,5]  
p a.x # =\> “x”

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 24, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595/5 "2007-06-24T12:28:45Z")

</div>

On 6/23/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):009:0\> a.x

Just reinclude Enumerable, well that seems to be the simplest way to  
achieve what you wanted:

> 

510/10 \> irb  
irb(main):001:0\> module A; def x; 42 end end  
=\> nil  
irb(main):002:0\> module Enumerable; include A end  
=\> Enumerable  
irb(main):003:0\> class Array; include Enumerable end  
=\> Array  
irb(main):004:0\> [].x  
=\> 42

And yes it works outside irb too.

Robert

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 24, 2007, 12:48pm UTC](https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595/6 "2007-06-24T12:48:46Z")

</div>

On Jun 24, 4:37 am, “Pit C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> end
> 
> a = [1,2,3,4,5]  
> p a.x # =\> “x”
> 
> Regards,  
> Pit

Pit Smack Down! You go and solve the Double Inclusion Problem just  
like that!?

There must be a large issue. Why wouldn’t Matz have already pursued  
this? Granted, I see a little inefficiency, but nothing an Observer  
pattern couldn’t mitigate. What say you?

I’m Speechless.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 24, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595/7 "2007-06-24T17:58:21Z")

</div>

On Jun 24, 8:01 am, “Pit C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Tom, thanks for the kind words, but it’s really not so hard to find  
> just one solution to this problem. I can’t speak for Matz, but I think  
> he’s looking for a more efficient solution that doesn’t require a lot  
> of bookkeeping.

Okay, I’m being a bit hyperbolic. But you deserve it anyway. I was  
surprised to see a solution thrown up in a few lines of code. The  
impression I had developed over previous discourse was such a thing  
was not readily possible. Of course, now it seems obvious. I knew we  
could just re-include the module, I just never put two and two  
together.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [June 24, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/cant-include-a-module-in-enumerable/107595/8 "2007-06-24T14:02:51Z")

</div>

2007/6/24, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Pit Smack Down! You go and solve the Double Inclusion Problem just  
> like that!?
> 
> There must be a large issue. Why wouldn’t Matz have already pursued  
> this? Granted, I see a little inefficiency, but nothing an Observer  
> pattern couldn’t mitigate. What say you?
> 
> I’m Speechless.

Tom, thanks for the kind words, but it’s really not so hard to find  
just one solution to this problem. I can’t speak for Matz, but I think  
he’s looking for a more efficient solution that doesn’t require a lot  
of bookkeeping.

Regards,  
Pit
