Skip to content

Instantly share code, notes, and snippets.

@crescentrose
Created June 4, 2020 20:10
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
module SaysHi
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def hi_to
@hi_to ||= ""
end
def says_hi_to(name)
@hi_to = name
end
end
def say_hi
puts "Hello, #{self.class.hi_to}"
end
end
class HelloReader
include SaysHi
says_hi_to "dear reader"
end
class HelloWorld
include SaysHi
says_hi_to "world"
end
HelloReader.new.say_hi # => "Hello, dear reader"
HelloWorld.new.say_hi # => "Hello, world"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment