Reopening a module vs. Module.include

I am attempting to create a plugin, with some methods in my own module.
These would then be included to various pieces of active record. This works
fine when including them in classes, but when including them in a module,
the classes which include that module do not get the methods. These classes
do however get them when directly reopening the module. Is there any way
around this, or is it best to just reopen the module and define the new
methods?

Here is a stripped down example of the problematic code...

module A
  def test
    "test"
  end
end

class B
  include A
end

B.new.test

=> "test"

module C
  def test_two
    "test_two"
  end
end

A.send :include, C

B.new.test_two

NoMethodError: undefined method `test_two' for #<B:0xa7b8ddbc>

class D
  include A
end

D.new.test_two

=> "test_two"

HOWEVER....

module A
  def test_two
    "test_two_again"
  end
end

B.new.test_two

=> "test_two_again"

ยทยทยท

--
Mark Van Holstyn
mvette13@gmail.com
http://lotswholetime.com

Mark Van Holstyn schrieb:

I am attempting to create a plugin, with some methods in my own module.
These would then be included to various pieces of active record. This works
fine when including them in classes, but when including them in a module,
the classes which include that module do not get the methods. These classes
do however get them when directly reopening the module. Is there any way
around this, or is it best to just reopen the module and define the new
methods?
(...)

Mark, this is a known limitation of the current Ruby implementation. It has been discussed on the mailing lists. The only workaround I know is to re-include the module:

   class B
     include A
   end

   p B.new.test_two # => "test_two"

Regards,
Pit