|
|
|
|
|
by stakecounter
3900 days ago
|
|
Should Money.new(20, 'USD') == Money.new(2000, 'US Cents')? Or Money.new(20, 'USD') == Money.new(125, 'CNY') when ExchangeRateManager.getExchangeRate('CNY', 'USD') == 0.16? My point is that when performing these comparisons, it may be useful to use a more descriptive function like: boolean currenciesHaveSameWorth(Money m1, Money m2) And then a reader of the calling code might not have to look into the implementation to understand what the function is doing, whereas you definitely would when using == because == now means "whatever it's overridden to mean" |
|
Like someone else pointed out in this thread, designing APIs require consistency and good taste.
If I had to implement this API you code above would evaluate to `ArgumentError unknown currency "US Cents"`.
> Or Money.new(20, 'USD') == Money.new(125, 'CNY') when ExchangeRateManager.getExchangeRate('CNY', 'USD') == 0.16?
Again, me designing this API, it wouldn't be equal. Why? For the same reason `1 != "1"`, if you cast them, yes they are equal, but implicit casting (aka weak typing) is not idiomatic in Ruby, it's possible, but very rare.
> boolean currenciesHaveSameWorth(Money m1, Money m2)
At this point you might as well do `m1 == m2.convert_to(m1.currency)`, because "HaveSameWorth" might mean many different things too.