require File.dirname(__FILE__) + '/../lib/add_hook' def main_method first_arg, second_arg, *splat_of_args end describe Object, '#add_hook' do it 'should be able to add a hook to an object in the main scope' do times_called = 0 main_method 1, 2, 3, 4, 5 times_called.should == 0 add_hook(:main_method) do |obj, *args| # puts "called main_method on #{ obj } with args: #{ args.inspect }" times_called += 1 end times_called.should == 0 main_method 1, 2, 3, 4, 5 times_called.should == 1 end it 'should be able to add a hook to objects' do object = [] times_called = 0 object << 'x' times_called.should == 0 object.add_hook(:<<){|obj, *args| times_called += 1 } object << 'x' times_called.should == 1 object << 'x' times_called.should == 2 # should not effect other arrays object = [] object << 'x' times_called.should == 2 # should still be 2 end end