-
-
Save cesc1989/f91be1fa7d6091bede0426753ba8eb99 to your computer and use it in GitHub Desktop.
Cómo se escriben las macros en Ruby
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 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