Skip to content

Instantly share code, notes, and snippets.

@eprothro
Last active August 20, 2018 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eprothro/7277b030693081e5e006ac8e1498b131 to your computer and use it in GitHub Desktop.
Save eprothro/7277b030693081e5e006ac8e1498b131 to your computer and use it in GitHub Desktop.
Ruby instrumentation glue example
module Instrumentation

  def action(*args)
    super.tap do |r|
      puts "#=> instrumented action: succes=#{r} args=#{args}"
    end
  end
end

class Base
  prepend Instrumentation

  def self.inherited(subclass)
    subclass.prepend Instrumentation
  end

  def action(arg)
    # default implementation that can be overridden
    false
  end
end

class SubClass < Base

  def action(arg)
    puts "#=> some specific algorithm with #{arg}"
    true
  end
end

SubClass.new.action(:foo)
Base.new.action(:foo)
#=> instrumented action: succes=true args=[:foo]
#=> false

SubClass.new.action(:foo)
#=> some specific algorithm with foo
#=> instrumented action: succes=true args=[:foo]
=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment