Skip to content

Instantly share code, notes, and snippets.

@kirkytullins
Created February 5, 2014 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirkytullins/8819217 to your computer and use it in GitHub Desktop.
Save kirkytullins/8819217 to your computer and use it in GitHub Desktop.
method interception
module MethodInterception
def method_added(meth)
return unless (@intercepted_methods ||= []).include?(meth) && !@recursing
@recursing = true # protect against infinite recursion
old_meth = instance_method(meth)
define_method(meth) do |*args, &block|
puts 'before'
old_meth.bind(self).call(*args, &block)
puts 'after'
end
@recursing = nil
end
def before_filter(meth)
(@intercepted_methods ||= []) << meth
end
end
class Test
extend MethodInterception
before_filter(:length)
def length(param)
puts "inside length : #{param}"
end
end
t = Test.new
t.length("the param")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment