Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nonzero? does not return a boolean #223

Open
ryanseys opened this issue Jan 13, 2021 · 5 comments
Open

nonzero? does not return a boolean #223

ryanseys opened this issue Jan 13, 2021 · 5 comments

Comments

@ryanseys
Copy link
Member

[6] pry(main)> Money.new(1, 'USD').nonzero?
=> 0.1e1
[7] pry(main)> Money.new(0, 'USD').nonzero?
=> nil
@ryanseys
Copy link
Member Author

Looks like BigDecimal nonzero? doesn't return a Boolean but rather "Returns self if the value is non-zero, nil otherwise."

https://ruby-doc.org/stdlib-2.5.1/libdoc/bigdecimal/rdoc/BigDecimal.html#method-i-nonzero-3F

@ryanseys
Copy link
Member Author

For comparison, Money.zero? does return a boolean.

[20] pry(main)> Money.new(0, 'USD').zero?
=> true
[21] pry(main)> Money.new(1, 'USD').zero?
=> false
[22] pry(main)> Money.new(-1, 'USD').zero?
=> false

@ryanseys
Copy link
Member Author

I filed a bug against BigDecimal as well: ruby/bigdecimal#181

@bdewater
Copy link
Contributor

Quoting from ruby/bigdecimal#181 (comment)

This is the intentionally designed specification of Numeric#nonzero?.
See https://docs.ruby-lang.org/en/3.0.0/Numeric.html#method-i-nonzero-3F

It seems Money is not handling this correctly by delegating to BigDecimal and should override this method to return a Money.

@ryanseys
Copy link
Member Author

Yeah either return a Money to remain consistent with BigDecimal's semantics, or I would expect nonzero? to really return a boolean -- but that was shot down in BigDecimal for some specific usage they anticipate (which I don't necessarily agree with, but don't have the influence to change either)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants