Top Level Namespace
Defined Under Namespace
Modules: Interface Classes: Module
Instance Method Summary collapse
-
#interface(&block) ⇒ Object
The interface method creates an interface module which typically sets a list of methods that must be defined in the including class or module.
Instance Method Details
#interface(&block) ⇒ Object
The interface method creates an interface module which typically sets a list of methods that must be defined in the including class or module. If the methods are not defined, an Interface::MethodMissing error is raised.
A interface can extend an existing interface as well. These are called sub-interfaces, and they can included the rules for their parent interface by simply extending it.
Example:
# Require 'alpha' and 'beta' methods
AlphaInterface = interface{
required_methods :alpha, :beta
}
# A sub-interface that requires 'beta' and 'gamma' only
GammaInterface = interface{
extends AlphaInterface
required_methods :gamma
unrequired_methods :alpha
}
84 85 86 87 88 89 |
# File 'lib/interface.rb', line 84 def interface(&block) mod = Module.new mod.extend(Interface) mod.instance_eval(&block) mod end |