[rspec-users] Why can not a BigDecimal be compared to a Float via "==". How should I handle this???

Greg Hauptmann greg.hauptmann.ruby at gmail.com
Sun Jan 11 03:17:15 EST 2009


Hi,
Any suggestions on how to write an rspec expectation for equality when you
get a BigDecimal back.  I see that "big_decimal_variable.should == 123.23"
doesn't work as the ruby BigDecimal comparison (via ==) to the same Float
value gives false (not true as you'd expect).  For background / as an
example see below:

        ?> bd
        => #<BigDecimal:221832c,'-0.32303E3',8(12)>
        >> f
        => -323.03
        >> f.class
        => Float
        >> bd.should == f
        Spec::Expectations::ExpectationNotMetError: expected: -323.03,
             got: #<BigDecimal:221832c,'-0.32303E3',8(12)> (using ==)
                from
/opt/local/lib/ruby/gems/1.8/gems/rspec-1.1.11/lib/spec/expectations.rb:52:in
`fail_with'
                from
/opt/local/lib/ruby/gems/1.8/gems/rspec-1.1.11/lib/spec/matchers/operator_matcher.rb:46:in
`fail_with_message'
                from
/opt/local/lib/ruby/gems/1.8/gems/rspec-1.1.11/lib/spec/matchers/operator_matcher.rb:61:in
`__delegate_method_missing_to_given'
                from
/opt/local/lib/ruby/gems/1.8/gems/rspec-1.1.11/lib/spec/matchers/operator_matcher.rb:12:in
`=='
                from (irb):73
        >> bd == f
        => false
        >> f == bd
        => false

thanks
Greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090111/d2c3aa60/attachment.html>


More information about the rspec-users mailing list