Skip to content

Instantly share code, notes, and snippets.

@glucero
Last active December 14, 2015 23:38
Show Gist options
  • Save glucero/5166710 to your computer and use it in GitHub Desktop.
Save glucero/5166710 to your computer and use it in GitHub Desktop.
inheriting from a module, method by method
module Kernel
def inherit_from(namespace, **methods)
methods.each do |key, value|
self.send :define_method, value, &namespace.instance_method(key).bind(self)
end
end
end
module Greetings
def english; 'hello' end
def italian; 'buongiorno' end
def french; 'bonjour' end
def spanish; 'hola' end
end
class Person
inherit_from Greetings, english: :speak,
french: :speak_french
end
# (main) > p = Person.new
# => #<Person:0x007feddc966488>
# (main) > p.speak
# => "hello"
# (main) > p.speak_french
# => "bonjour"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment