Skip to content

Instantly share code, notes, and snippets.

@lucasmazza
Last active May 9, 2018 02:02
Show Gist options
  • Select an option

  • Save lucasmazza/91ae43bfea7e26782b0212ecb331ac15 to your computer and use it in GitHub Desktop.

Select an option

Save lucasmazza/91ae43bfea7e26782b0212ecb331ac15 to your computer and use it in GitHub Desktop.

Revisions

  1. lucasmazza revised this gist May 9, 2018. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions 01.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    ```
    $ ruby thing.rb
    [Calculator::LoggerDecorator, Calculator, Object, Kernel, BasicObject]
    Calculator#div(10, 2) => 5
    ```
  2. lucasmazza created this gist May 9, 2018.
    27 changes: 27 additions & 0 deletions mimito.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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)