Skip to content

Instantly share code, notes, and snippets.

@bil-bas
Created March 14, 2011 13:41
Show Gist options
  • Save bil-bas/869125 to your computer and use it in GitHub Desktop.
Save bil-bas/869125 to your computer and use it in GitHub Desktop.
before/after
class Object
class << self
# @example
# before(:initialize) do
# log.debug "Initializing chicken"
# end
def before(method_name, &block)
raise "Requires block" unless block_given?
original = instance_method(method_name)
define_method(method_name) do |*args, &runtime_block|
block.call *args, &runtime_block
original.bind(self).call *args, &runtime_block
end
end
# @example
# after(:initialize) do
# log.debug "Initialized chicken"
# end
def after(method_name, &block)
raise "Requires block" unless block_given?
original = instance_method(method_name)
define_method(method_name) do |*args, &runtime_block|
original.bind(self).call *args, &runtime_block
block.call *args, &runtime_block
end
end
# @example
# around(:play) do |volume = 1, speed = 1, looping = false, &block|
# block.call(volume * @@global_volume, speed, looping)
# end
def around(method_name, &block)
raise "Requires block" unless block_given?
original = instance_method(method_name)
define_method(method_name) do |*args, &runtime_block|
block.call(*args) do |*yielded_args|
original.bind(self).call *yielded_args, &runtime_block
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment