0.02
No commit activity in last 3 years
No release in over 3 years
Abstract Method is a tiny library enabling you to define abstract methods in Ruby classes and modules.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
 Project Readme

Abstract Method

Abstract Method is a tiny library enabling you to define abstract methods in Ruby classes and modules.

Example

class AbstractClass
  abstract_method :foo
end

class ConcreteClass < AbstractClass
  def foo
    42
  end
end

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

Installation

$ gem install abstract_method

Usage

First, require the library:

require "abstract_method"

You can now use the abstract_method method to define abstract methods in classes and modules.

class AbstractClass
  abstract_method :foo
end

class AbstractModule
  abstract_method :bar
end

When called, the abstract method will raise an AbstractMethodCalled exception with a helpful message:

AbstractClass.new.foo # raises AbstractMethodCalled with the following message:
                      #   Called unimplemented abstract method AbstractClass#foo
                      #   (defined in class AbstractClass).

Abstract methods can be overridden as usual:

class ConcreteClass < AbstractClass
  def foo
    42
  end
end

ConcreteClass.new.foo # => 42

For more information, see the API documentation.

FAQ

Why define abstract methods? In a dynamic language like Ruby they aren't needed.

Mainly for documentation purposes. The definitions allow subclass authors to easily see what they need to implement. They are also a great place to attach documentation.

Why create a new library? There is already the abstract gem.

With the abstract gem you have to specify the abstract method parameter list in a string when you define it. This is ugly and in Ruby the parameter list often does not say much about the method anyway. The parameters should should rather be described using a code documentation system (such as YARD).