Created
September 30, 2010 05:47
-
-
Save ryanbriones/604091 to your computer and use it in GitHub Desktop.
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
| module TemporarilyExtend | |
| def self.track_method(klass, method) | |
| @methods ||= {} | |
| @methods[klass] ||= [] | |
| @methods[klass] << method | |
| end | |
| def self.tracked_methods(klass) | |
| @methods ||= {} | |
| @methods[klass] | |
| end | |
| def self.clear_tracked_methods(klass) | |
| @methods ||= {} | |
| if @methods[klass] | |
| @methods[klass].each do |meth| | |
| klass.send(:remove_method, meth) | |
| end | |
| end | |
| end | |
| def method_added(meth) | |
| TemporarilyExtend.track_method(self, meth) | |
| end | |
| def singleton_method_added(meth) | |
| return if meth == :singleton_method_added | |
| TemporarilyExtend.track_method(self, meth) | |
| end | |
| end | |
| def extending(klass) | |
| klass.class_eval do | |
| extend TemporarilyExtend | |
| end | |
| yield | |
| TemporarilyExtend.clear_tracked_methods(klass) | |
| end | |
| a = "foo" | |
| extending(String) do | |
| class String | |
| def bar | |
| puts "bar" | |
| end | |
| end | |
| a.bar | |
| end | |
| a.bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment