Skip to content

Instantly share code, notes, and snippets.

@stevenjack
Created June 5, 2014 09:01
Show Gist options
  • Save stevenjack/5179795998028e6a0703 to your computer and use it in GitHub Desktop.
Save stevenjack/5179795998028e6a0703 to your computer and use it in GitHub Desktop.
module AOP
def around(fn_name)
old_method = instance_method(fn_name)
define_method(fn_name) do |*args|
yield :before, args if block_given?
old_method.bind(self).call *args
yield :after, args if block_given?
end
end
end
class Foo
extend AOP
def process(msg)
puts msg
end
end
Foo.around('process') do |state, args|
if state == :before
puts 'Blah 1'
elsif state == :after
puts 'Blah 2'
end
end
Foo.new.process('hai')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment