Last active
December 14, 2015 23:38
-
-
Save glucero/5166710 to your computer and use it in GitHub Desktop.
inheriting from a module, method by method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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