Skip to content

Instantly share code, notes, and snippets.

@ryanbriones
Created September 30, 2010 05:47
Show Gist options
  • Select an option

  • Save ryanbriones/604091 to your computer and use it in GitHub Desktop.

Select an option

Save ryanbriones/604091 to your computer and use it in GitHub Desktop.
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