Created
January 8, 2015 21:29
-
-
Save bitops/083e8663c64ecdf8de61 to your computer and use it in GitHub Desktop.
A simple decorator in Ruby powered by method_missing
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
| 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