[Rspec-devel] possible addition to expectations...

Richard Kilmer rich at infoether.com
Mon Apr 24 07:45:29 EDT 2006

Hello all.

Very great framework!

I was talking with Steven Baker at the Ruby in the Valley thing and I  
came up with a simple addition that allows underscore notation in  
addition to dot notation...so you can do this:

target.should.equal <value>
target.should.not.equal <value>
target.should.be.close <value>, <tolerance>
target.should.not.be.close <value>, <tolerance>

can also be expressed as:

target.should_equal <value>
target.should_not_equal <value>
target.should_be_close <value>, <tolerance>
target.should_not_be_close <value>, <tolerance>

What I did was override method_missing on Object and if the call  
begins with should_ it splits on _ and then chain invokes those  
methods.  There may be cleaner code than this...I did it on a red-eye.


Rich Kilmer

class Object
   include Spec::ObjectExpectations

   alias_method :__orig_method_missing, :method_missing
   def method_missing(method, *args, &block)
     if method.to_s[0,7] == "should_"
       object = self
       calls = method.to_s.split("_")
       while calls.length > 1
         object = object.send(calls.shift)
       return object.send(calls.shift, *args, &block)
     __orig_method_missing(method, *args, &block)


More information about the Rspec-devel mailing list