Skip to content

Instantly share code, notes, and snippets.

@maxwells
Created October 20, 2017 21:11
Show Gist options
  • Save maxwells/b37d6ff772b7a8a660d351de0a59f066 to your computer and use it in GitHub Desktop.
Save maxwells/b37d6ff772b7a8a660d351de0a59f066 to your computer and use it in GitHub Desktop.
garbage ruby decorators
module Decorator
def def_decorator(name, &blk)
define_method(name) do |method_name|
new_name = "#{method_name}_"
alias_method new_name, method_name
define_method(method_name) do
lmbda = lambda { send(new_name) }
blk.call(lmbda)
end
new_name
end
end
end
module TimingDecorator
extend Decorator
def_decorator(:time) do |lambda|
puts Time.now.to_i
lambda.call
puts Time.now.to_i
end
end
class Thing
extend TimingDecorator
time def do_the_thing
puts "doing the thing"
end
end
Thing.new.do_the_thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment