Testing the <=> operator

I’m having trouble getting rspec to allow me to test my Comparable
class. I
would like to test the <=> method, but rspec is giving me unexpected
results:

@hand = Hand.new(‘R’)
@other_hand = Hand.new(‘S’)
@hand.should be > @other_hand

expected > #RPS::Hand:0x7f2d93661490, got #<RPS::hand:0x7f2d936615d0
@guess=“R”>

Shouldn’t that test be checking for the comparison operator?

chris

On Mar 15, 2010, at 11:16 PM, Chris DiMartino wrote:

I’m having trouble getting rspec to allow me to test my Comparable class. I would like to test the <=> method, but rspec is giving me unexpected results:

@hand = Hand.new(‘R’)
@other_hand = Hand.new(‘S’)
@hand.should be > @other_hand

expected > #RPS::Hand:0x7f2d93661490, got #<RPS::hand:0x7f2d936615d0 @guess=“R”>

Shouldn’t that test be checking for the comparison operator?

Nope. Keep in mind that most operators in ruby are actually methods.
When the example says:

@hand.should be > @other_hand

Ruby evaluates that as this:

@hand.should(be.>(@other_hand))

Which rspec then translates to this (more or less):

@hand.>(@other_hand) ? pass : fail

That make sense?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs