2010/7/25 Rolf Pedersen <rolfhsp / gmail.com>:
> module Interface
> =A0module ILockable
> =A0 =A0add_interface_methods :lock, :unlock
> =A0end
>
> =A0module IDrivable
> =A0 =A0add_interface_methods :drive, :break
> =A0end
> end

add_interface_methods is not defined for these modules.
"include Base" in both should remove this error, but I'm not sure
that's what you want in the end.

I take the opportunity of this topic to provide my own version of
interface implementation and to ask a question about it (if it's off
topic, don't hesitate to tell me). My goal is to use your code to
perform an automatic interface check and not bother with calling
enforce_interface everytime:

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
module Interface
  def inherited(base)
	puts "#{base} inherited from me"
	enforce_interface(base)
  end

  def enforce_interface(base)
	instanceMethods =3D base.public_instance_methods()

	if (!@interface_methods.nil?())
	  @interface_methods.each() do |method|
		throw "No method #{method}" unless instanceMethods.include?(method)
	  end
	end
  end

  def add_interface_methods(*methods)
       methods.each do |method|
         (@interface_methods ||=3D []) << method.to_s
       end
  end
end

class Mother
	extend Interface

	add_interface_methods :pwet, :ziou
end

class Child < Mother
	def pwet()
		puts "Method pwet"
	end

	def initialize()
		puts "Child ctor"
	end
end
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

This piece of code almost works (so, doesn't work :-)), but ends up
(logically) with a "No method pwet" exception. I suppose that it's due
to the fact that Child inherits from Mother before to be able to
define any method. Is there a way around this problem ?

I like Paolo's proposal (which could maybe be extended to allow to
check methods arity as well), but dislike the need to call explicitely
enforce_interface.

--=20
Xavier NOELLE