Skip to content

Instantly share code, notes, and snippets.

@tdg5
Created April 15, 2015 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdg5/99a7ec0ddda0e1729a04 to your computer and use it in GitHub Desktop.
Save tdg5/99a7ec0ddda0e1729a04 to your computer and use it in GitHub Desktop.
PartiallyImplementedInterface mixin
module PartiallyImplementedInterface
def self.not_implemented_error(klass, method)
raise NotImplementedError, not_implemented_message(klass, method), caller(2)
end
def self.not_implemented_message(klass, method_name)
"#{method_name} must be implemented by subclasses of #{klass.name}!"
end
private_class_method :not_implemented_message
def not_implemented(*methods)
methods.each do |iface_method|
define_method(iface_method) do
PartiallyImplementedInterface.not_implemented_error(self.class, iface_method)
end
end
end
end
class Meow
extend PartiallyImplementedInterface
not_implemented :nesting
def lots_of_nesting; lots; end
def lots; of; end
def of; nesting; end
end
Meow.new.lots_of_nesting
# example.rb:27:in `of': nesting must be implemented by subclasses of Meow! (NotImplementedError)
# from example.rb:26:in `lots'
# from example.rb:25:in `lots_of_nesting'
# from example.rb:30:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment