Bug in the money gem?

For anyone of you using the money gem (http://dist.leetsoft.com/api/
money/), I noticed today that 0 - 100 does not equal -100, it equals
100 instead (all numbers here represent Money objects). Do any of you
find that odd?

Eric

On Thu, Sep 25, 2008 at 2:34 PM, Eric LIn [email protected] wrote:

For anyone of you using the money gem (Too-biased
money/), I noticed today that 0 - 100 does not equal -100, it equals
100 instead (all numbers here represent Money objects). Do any of you
find that odd?

I’m not sure, but it seems a bit strange. Here is the offending code:

# File lib/money/money.rb, line 79

79: def -(other_money)
80: return other_money.dup if cents.zero?
81: return dup if other_money.cents.zero?
82:
83: if self.cents == 0 or currency == other_money.currency
84: Money.new(cents - other_money.cents, other_money.currency)
85: else
86: Money.new(cents - other_money.exchange_to(currency).cents,
currency)
87: end
88: end

I think the first return would make sense if the other_money variable
had a negative value associated with it, however, other_money’s value
is not negative.

HTH,
Michael G.

On Sep 25, 2:32 pm, Eric LIn [email protected] wrote:

For anyone of you using the money gem (Too-biased
money/), I noticed today that 0 - 100 does not equal -100, it equals
100 instead (all numbers here represent Money objects). Do any of you
find that odd?

Yes, it’s odd. The problem is line 80:

File lib/money/money.rb, line 79

79: def -(other_money)
80: return other_money.dup if cents.zero?
81: return dup if other_money.cents.zero?
82:
83: if self.cents == 0 or currency == other_money.currency
84: Money.new(cents - other_money.cents, other_money.currency)
85: else
86: Money.new(cents - other_money.exchange_to(currency).cents,
currency)
87: end
88: end

Try commenting line 80 out. I don’t see any reason why the class
wouldn’t handle negative values, though I didn’t look carefully.

– Mark.

On Sep 25, 12:04 pm, Michael G. [email protected] wrote:

80: return other_money.dup if cents.zero?
had a negative value associated with it, however, other_money’s value
is not negative.

HTH,
Michael G.

Thanks for the response, I figured that’s the problem too. It seems
the author made the mistake unintentionally, when he copies that line
of code from the “+” method. Thanks for confirming my suspicion; I
was thinking maybe it’s something in currency arithmetic that I don’t
understand. I’ve emailed the author, but so far no response.