Created
March 15, 2018 03:55
-
-
Save flanker/752aaccba82de966d8aa2bd68b217dd6 to your computer and use it in GitHub Desktop.
tiny example of ruby module usage
This file contains 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 Sayable | |
def self.included(host_class) | |
host_class.extend ClassMethods | |
end | |
module ClassMethods | |
def sound sound | |
@the_sound = sound | |
end | |
def the_sound | |
@the_sound | |
end | |
end | |
def say | |
puts self.class.the_sound | |
end | |
end | |
class Dog | |
include Sayable | |
sound 'bark' | |
end | |
class Cat | |
include Sayable | |
sound 'meow' | |
end | |
dog = Dog.new | |
dog.say | |
# ===> 'bark' | |
cat = Cat.new | |
cat.say | |
# ===> 'meow' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment