3

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!

2
  • What are you trying to achieve? Mar 12, 2012 at 8:26
  • I didn't think that mattered for the question at hand. But, I'm trying to add a palindrome? method for enumerables for a homework assignment. I understand that it's silly for things like Hashes, but it's what I'm trying to do.
    – Nathan
    Mar 12, 2012 at 8:45

1 Answer 1

3

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!"
1
  • Yep, that makes a lot of sense. Thanks.
    – Nathan
    Mar 13, 2012 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.