Created
May 28, 2011 16:26
-
-
Save etiennebarrie/997005 to your computer and use it in GitHub Desktop.
method_missing
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
class InfoDesk | |
def trains | |
:trains | |
end | |
def flights | |
:flights | |
end | |
def buses | |
:buses | |
end | |
end | |
class Clock | |
def self.lunch_time? | |
true | |
end | |
end | |
class Sign < BasicObject | |
def initialize | |
@desk = ::InfoDesk.new | |
end | |
def method_missing(method, *args, &block) | |
return super unless @desk.respond_to?(method) | |
singleton_class = class << self; self; end | |
singleton_class.send(:define_method, method) do | |
return 'out to lunch' if ::Clock.lunch_time? | |
@desk.send(method, *args, &block) | |
end | |
__send__(method, *args, &block) | |
end | |
def respond_to?(method) | |
@desk.respond_to?(method) || super | |
end | |
end | |
sign = Sign.new | |
p sign.respond_to?(:trains) | |
p sign.trains |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment