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
[nil].sum returns 0 #24796
Comments
This is what we rely on and what ruby does
Since the end result is nil, we fallback to 0- vipulnsward@5241b97 @mrkn I am not sure if that is intentional though. |
@mrkn I see we behave differently for ➜ ruby git:(trunk) ✗ ./ruby -ve 'puts [nil].inject(:+)'
ruby 2.4.0dev (2016-04-30 trunk 54853) [x86_64-darwin15]
➜ ruby git:(trunk) ✗ ./ruby -ve 'puts [nil, 1].inject(:+)'
ruby 2.4.0dev (2016-04-30 trunk 54853) [x86_64-darwin15]
-e:1:in `inject': undefined method `+' for nil:NilClass (NoMethodError)
from -e:1:in `<main>'
➜ ruby git:(trunk) ✗ ./ruby -ve '[nil].sum'
ruby 2.4.0dev (2016-04-30 trunk 54853) [x86_64-darwin15]
-e:1:in `+': nil can't be coerced into Fixnum (TypeError)
from -e:1:in `sum'
from -e:1:in `<main>'
➜ ruby git:(trunk) ✗ |
In Ruby 2.4, sum isn't identical to
The other difference is the initial value effect. I want to resolve the inconsistency of the behavior of |
In other words, I want to fix this if it isn't the desired behavior. |
We don't have a specification for this. cc @jeremy |
Great edge case. The |
Oops! I see #24795 does not address this case. Agree: behavior here is undefined. Expected behavior is I think this is buggy, since |
Is this buggy? I think it's perfectly reasonable for this to match the behavior of |
I think we could handle it. Generally, in our Apps, someone would do |
…false, matching inject(:+) results
probably related... before
after:
That’s life. sigh and replace my code |
Confirmed that this is still an issue in Rails 5.2.1
But in ruby 2.5:
|
Just to make sure the variant is covered: |
The latest version of Ruby/Rails ( |
|
Is this intentional?
Steps to reproduce
Expected behavior
I don't have the idea what is expected, ideal behavior.
But
[nil].sum
raises TypeError on Ruby 2.4.Actual behavior
Returns 0.
System configuration
Rails version: 151080a
Ruby version: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
The text was updated successfully, but these errors were encountered: