Skip to content

Instantly share code, notes, and snippets.

@bitops
Created January 8, 2015 21:29
Show Gist options
  • Select an option

  • Save bitops/083e8663c64ecdf8de61 to your computer and use it in GitHub Desktop.

Select an option

Save bitops/083e8663c64ecdf8de61 to your computer and use it in GitHub Desktop.
A simple decorator in Ruby powered by method_missing
class Thing
def foo
puts "I'm a foo"
end
def bar
puts "I'm a bar"
end
def baz
puts "I'm a baz"
end
end
class ThingDecorator
def initialize
@thing = Thing.new
end
def method_missing(method_name)
if @thing.respond_to?(method_name)
@thing.send(method_name)
else
puts "Can't call #{method_name.to_sym} as it doesn't exist on thing"
end
end
end
td = ThingDecorator.new
td.foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment