Skip to content
Daniel Berger edited this page May 4, 2022 · 6 revisions

This library was created as a proof of concept many years ago to see if we could implement Java style interfaces for Ruby, and how we would go about it exactly. The discussions were largely between David Naseby, Mauricio Fernandez and myself, and this library is mostly a combination of David's and Mauricio's ideas.

I personally was not a fan of interfaces at the time. It seemed to me that modules, with methods that already had defined functionality, were an obviously better choice. However, I've come around to the idea of interfaces for Ruby, since larger projects with pluggable architectures depend on them. These projects simply cannot define generic functionality via a module, since the implementation will vary too much from plugin to plugin.

Anyway, here you go.

Sample interface.

  require 'interface'
	
  MyInterface = interface{
    required_methods :foo, :bar, :baz
  }
	
  # Raises an error until 'baz' is defined
  class MyClass
    def foo
      puts "foo"
    end

    def bar
      puts "bar"
    end

    implements MyInterface
  end

For a very Ruby-like language that effectively implemented interfaces, please see Crystal's abstract keyword:

https://crystal-lang.org/reference/1.4/syntax_and_semantics/virtual_and_abstract_types.html

Clone this wiki locally