Skip to content

Instantly share code, notes, and snippets.

@flanker
Created March 15, 2018 03:55
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 flanker/752aaccba82de966d8aa2bd68b217dd6 to your computer and use it in GitHub Desktop.
Save flanker/752aaccba82de966d8aa2bd68b217dd6 to your computer and use it in GitHub Desktop.
tiny example of ruby module usage
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