Skip to content
rhook - Easily drive AOP & hacking existing library with Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


rhook - Easily drive AOP & hacking existing library with Ruby

  • You can provide hook point in your code,

    • and can customize its behavior from outside.

  • Also you can 'hack' (== injecting hook point from outside) any methods in existing code.


gem install rhook

Basic Usage

step. Example code

class TargetBasic
  def greeting
    my_name = "Mike"
    your_name = "Dan"
    hello(your_name) + "I am #{my_name}."

  def hello(name)
    "Hello, #{name}. "
end #/TargetBasic

that behaves as:

t =
t.greeting.should == "Hello, Dan. I am Mike."

step. Insert hook point in TargetBasic

require "rhook"      # <==   load library.

class TargetBasic
  def greeting
    my_name = _rhook.does(:my_name) { "Mike"; }         # <==  my_name = "Mike"
    your_name = "Dan" + "I am #{my_name}."         # <==  hello(your_name) + "I am #{my_name}."

  def hello(name)
    "Hello, #{name}. "
end #/TargetBasic

step. Add hook from outside, and change behavior.

t =
t._rhook.bind(:hello) { |inv|
  inv.args[0] = "Jenkins"     # change first argument.
t._rhook.bind(:my_name) { |inv|
  "Katherine"     # change return value.

that behaves as:

t.greeting.should == "Hello, Jenkins. I am Katherine."
                             ~~~~~~~       ~~~~~~~~~

More Basic TIPs

about '_rhook'

Once you require 'rhook', any objects has '_rhook' method to access any rhook services. (that returns RHook::RHookService object.)

any_object =

What is 'inv' ?

RHook::Invocation object, that contains:

  • call() method to proceed method invocation.

  • receiver

  • arguments passed to method

  • blocks passed to method

  • returned value by call()

If you want 'bind' to not only an object, but any instances of class:

TargetBasic._rhook.bind(:hello) { |inv| ...

If you want to HACK exisitng methods, use '_rhook.hack':

for the paticular instance:

t =
t._rhook.hack(:to_s) do |inv|
  "haha! I hacked to_s."
t.to_s.should == "haha! I hacked."

for entire class:

Time._rhook.hack(:to_s) do |inv|
  "haha! I hacked to_s."
( == "haha! I hacked to_s."

for class method:

Time._rhook.hack(:now) do |inv|
  "haha! you cannot get time."
end == "haha! you cannot get time."

More Usage …

Please see spec:

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.


Copyright © 2010 Kaoru Kobo. See LICENSE for details.

Something went wrong with that request. Please try again.