Skip to content

Instantly share code, notes, and snippets.

@haileys
Last active December 24, 2015 01:29
Show Gist options
  • Save haileys/6723428 to your computer and use it in GitHub Desktop.
Save haileys/6723428 to your computer and use it in GitHub Desktop.
# Method instrumentation decorator with Ruby 2.1's new "method definition returns method name" feature
class Module
def instrument(notification_name, mid)
meth = instance_method(mid)
define_method(mid) { |*args, &bk|
ActiveSupport::Notifications.instrument(notification_name) do
meth.bind(self).call(*args, &bk)
end
}
mid
end
end
class MyClass
instrument "my_class.some_expensive_method",
def some_expensive_method
# do things
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment