From: Gregory Brown Date: 2007-06-24T05:46:49+09:00 Subject: Re: Can't include a module in Enumerable? On 6/23/07, Trans wrote: > 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? 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