# Bug in the money gem?

**URL:** <https://www.ruby-forum.com/t/bug-in-the-money-gem/149529>\
**Category:** Ruby\
**Created:** [September 25, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/bug-in-the-money-gem/149529 "2008-09-25T20:36:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [September 25, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/bug-in-the-money-gem/149529/1 "2008-09-25T20:36:04Z")

</div>

For anyone of you using the money gem ([http://dist.leetsoft.com/api/](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

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [September 25, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/bug-in-the-money-gem/149529/2 "2008-09-25T21:05:31Z")

</div>

On Thu, Sep 25, 2008 at 2:34 PM, Eric LIn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For anyone of you using the money gem ([Too-biased](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?

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.

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [September 25, 2008, 8:49pm UTC](https://www.ruby-forum.com/t/bug-in-the-money-gem/149529/3 "2008-09-25T20:49:38Z")

</div>

On Sep 25, 2:32 pm, Eric LIn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For anyone of you using the money gem ([Too-biased](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?

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.

---

<div class="post-metadata">

**Author:** ![Eric\_LIn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/45deac/32.png) [@Eric\_LIn](https://www.ruby-forum.com/u/Eric_LIn)\
**Post date:** [October 1, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/bug-in-the-money-gem/149529/4 "2008-10-01T22:50:56Z")

</div>

On Sep 25, 12:04 pm, Michael G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
