Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/abstract_method.rb

Instance Method Summary collapse

Instance Method Details

#abstract_method(*names) ⇒ Object

Defines one or more abstract methods with given names in a class or module. When called, the abstract method will raise an AbstractMethodCalled exception with a helpful message.

Examples:

class AbstractClass
  abstract_method :foo
end

class ConcreteClass < AbstractClass
  def foo
    42
  end
end

AbstractClass.new.foo # raises AbstractMethodCalled
ConcreteClass.new.foo # => 42

Parameters:

  • names (Array<Symbol>)

    the names of defined abstract methods


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/abstract_method.rb', line 32

def abstract_method(*names)
  definitor = self

  names.each do |name|
    define_method name do |*args|
      raise AbstractMethodCalled,
            "Called unimplemented abstract method #{self.class}##{name} " +
            "(defined in #{definitor.class.name.downcase} #{definitor})."
    end
  end
end