Skip to content

yegor256/intercepted

Repository files navigation

Intercepts All Method Calls to a Ruby Object

DevOps By Rultor.com We recommend RubyMine

rake PDD status Gem Version Test Coverage Yard Docs Hits-of-Code License

It decorates an existing Ruby object and intercepts all calls to all its methods:

require 'intercepted'
i = intercepted(42) do |e, m, args, r|
  if e == :after
    puts "#{m}(#{args.join(', ')}) -> #{r}"
  end
end
puts i + 4

Prints:

+(4) -> 46

The value of e may either be :after or :before. The value of m is the name of the method called (a symbol). The list of arguments passed are in the args. The result of method execution is in the r (it's nil when e is :before).

That's it.

How to contribute

Read these guidelines. Make sure your build is green before you contribute your pull request. You will need to have Ruby 3.0+ and Bundler installed. Then run:

bundle update
bundle exec rake

If it's clean and you don't see any error messages, submit your pull request.