rails: [nil].sum returns 0
Is this intentional?
Steps to reproduce
$ pwd
/Users/mrkn/work/rails/master/activesupport
$ git rev-parse --short HEAD
151080a
$ ruby -v -I lib -r active_support/core_ext/enumerable -e 'p [nil].sum'
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
0
Expected behavior
I don’t have the idea what is expected, ideal behavior.
But [nil].sum
raises TypeError on Ruby 2.4.
$ ruby -ve '[nil].sum'
ruby 2.4.0dev (2016-04-28 trunk 54810) [x86_64-darwin15]
-e:1:in `+': nil can't be coerced into Fixnum (TypeError)
from -e:1:in `sum'
from -e:1:in `<main>'
Actual behavior
Returns 0.
System configuration
Rails version: 151080a
Ruby version: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 1
- Comments: 16 (12 by maintainers)
Commits related to this issue
- Fixes issue #24796 -- Makes [nil].sum == nil and [false].sum == false, matching inject(:+) results — committed to khall/rails by deleted user 8 years ago
Confirmed that this is still an issue in Rails 5.2.1
But in ruby 2.5: