-
-
Notifications
You must be signed in to change notification settings - Fork 5
Home
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 was mostly a combination of David's and Mauricio's ideas originally. It has since been enhanced by AI.
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 do
required_methods :foo, :bar, :baz
end
# Raises an error until 'baz' is defined
class MyClass
implements MyInterface
def foo
puts "foo"
end
def bar
puts "bar"
end
endFor a variation on this library, please see "rubycube" at https://github.com/oogway/rubycube.
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
As of version 1.2.0, you no longer have to defer the implements declaration until after method declarations. This, and some general refactoring, is courtesy of Ruby's TracePoint class and Claude Sonnet 4.