Created
August 12, 2016 21:27
-
-
Save jquave/39e4c5fb1f060d2a9bcf35020878bbe7 to your computer and use it in GitHub Desktop.
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
# Framework Object::say_hello | |
class Object | |
def say_hello | |
"hello" | |
end | |
end | |
# Your Object class | |
class Object | |
module InSpanish | |
def say_hello | |
"hola" | |
end | |
end | |
include InSpanish | |
module InEnglish | |
expects_method = Object.instance_method(:say_hello) | |
define_method :say_hello do |*args| | |
expects_method.bind(self).call(*args) | |
end | |
end | |
include InEnglish | |
def say_hello | |
(@language || InEnglish).instance_method(:say_hello).bind(self).call | |
end | |
def in_english(&block) | |
@language = InEnglish | |
instance_eval(&block) | |
end | |
def in_spanish(&block) | |
@language = InSpanish | |
instance_eval(&block) | |
end | |
end | |
in_english do | |
say_hello # => "hello" | |
end | |
in_spanish do | |
say_hello # => "hola" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment