From: Paolo Perrotta Date: 2010-07-26T07:45:07+09:00 Subject: Re: Module inclusion and class macro problem Hello, Rolf. Let's wrap up what happened and why. The issue comes from these lines: > module Interface >   module ILockable >     add_interface_methods :lock, :unlock >   end > >   # ... > end Coming from C#, you're probably tempted to assume that the code inside ILockable (and IDrivable) is executed only when a class/module is "loaded". Actually, that code is executed immediately as Ruby goes through the definition. So, at this point (and independently of all the other code in your example), Ruby tries to execute add_interface_methods() in the scope of Interface::ILockable. Inside that scope, the role of self is taken by Interface::ILockable itself - so that's the implicit receiver of any method call. So Ruby tries to call Interface::ILockable.add_interface_methods(), doesn't find that method, and fails. In the real world, I'd go for something simpler (and I'd be a real prick in questioning the notion of strongly typed interfaces in Ruby ;) ). However, because this is a great learning exercise, here's something along your own lines that probably works as you expect: --- module Interface module Base def self.included(base) base.extend ClassMethods end module ClassMethods def implement_interfaces(*interfaces) interfaces.each do |interface| @interface_methods = (@interface_methods || []) + interface.instance_methods puts "self: " + self.inspect.to_s end end def enforce_interfaces if not (not_implemented_interface_methods = @interface_methods - self.instance_methods).empty? raise RuntimeError, "The following interface methods are not implemented in class #{self.to_s}:\n " +not_implemented_interface_methods.inspect end end end end end module Interface module ILockable def lock; end def unlock; end end module IDrivable def drive; end def end; end end end class MyClass include Interface::Base implement_interfaces Interface::ILockable, Interface::IDrivable # methods and other stuff enforce_interfaces end --- Paolo "Nusco" Perrotta Metaprogramming Ruby (http://www.pragprog.com/titles/ppmetr)