Skip to content

Instantly share code, notes, and snippets.

@etiennebarrie
Created May 28, 2011 16:26
Show Gist options
  • Save etiennebarrie/997005 to your computer and use it in GitHub Desktop.
Save etiennebarrie/997005 to your computer and use it in GitHub Desktop.
method_missing
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