Skip to content

Instantly share code, notes, and snippets.

@lucasmazza
Last active May 9, 2018 02:02
Show Gist options
  • Save lucasmazza/91ae43bfea7e26782b0212ecb331ac15 to your computer and use it in GitHub Desktop.
Save lucasmazza/91ae43bfea7e26782b0212ecb331ac15 to your computer and use it in GitHub Desktop.
$ ruby thing.rb
[Calculator::LoggerDecorator, Calculator, Object, Kernel, BasicObject]
Calculator#div(10, 2) => 5
module MagicLog
def self.extended(receiver)
decorator = Module.new
receiver.prepend decorator
receiver.const_set(:LoggerDecorator, decorator)
end
def log(name)
self::LoggerDecorator.define_method(name) do |*args|
result = super(*args)
puts "#{self.class.name}##{name}(#{args.map(&:inspect).join(", ")}) => #{result}"
result
end
end
end
class Calculator
extend MagicLog
log :div
def div(x, y)
x / y
end
end
p Calculator.ancestors
Calculator.new.div(10, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment