Nathan
Nathan

Reputation: 1762

include a new mixin in all enumerable classes

I'd like to add a module to all Enumerable classes. Is there a good way to do this?

My solution at this point is:

module Enumerable
  include my_module
end

class Array
  include Enumerable
end

class ____
etc...

Unless I include the new version of Enumerable in all of the classes which include the original Enumerable, they do not get updated. Is there a better way of doing this? Perhaps with some meta programming?

Thanks!

Upvotes: 3

Views: 489

Answers (1)

nolith
nolith

Reputation: 613

As far as I know you have to write the method code inside the Enumerable module

module Enumerable
    def so
        "StackOverflow!"
    end
end

a = [1, 3, 7]
a.so
#=> "StackOverflow!"

Upvotes: 3

Related Questions