views:

162

answers:

1

Hi,

I was reading up on rspec and I was trying to figure out how rspec's "should" was implemented

Could someone give a hand on how the meta nature of this function works?

The code is located here:

http://github.com/dchelimsky/rspec/blob/master/lib/spec/expectations/extensions/kernel.rb

TIA,

-daniel

Clarification:

target.should == 5

How did target's value get passed along to "should", which in turn was "=="'d against 5?

+8  A: 

Take a look at class OperatorMatcher.

It all boils down to Ruby allowing you to leave out periods and parenthesis. What you are really writing is:

target.should.send(:==, 5)

That is, send the message should to the object target.

The method should is monkey patched into Kernel, so it can be received by any object. The Matcher returned by should holds the actual which in this case is target.

The Matcher implements the method == which does the comparison with the expected which, in this case, is the number 5. A cut down example that you can try yourself:

module Kernel
  def should
    Matcher.new(self)
  end
end

class Matcher
  def initialize(actual)
    @actual = actual
  end

  define_method(:==) do |expected|
    if @actual == expected
      puts "Hurrah!"
    else
      puts "Booo!"
    end
  end
end

target = 4
target.should == 5
=> Booo!

target = 5
target.should == 5
=> Hurrah!
Bryan Ash
Thanks fo a great little explanation that not only answers the question but is a nice summary of some mechanisms in Ruby. For people new to Ruby, be aware that the last expression in a function is implicitly the return value, so should above would in C++ be Matcher* Kernel::should(){ return new Matcher(this);}
Andy Dent
@Andy Dent: thank you for the positive comment
Bryan Ash