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

Most upvoted comments

Confirmed that this is still an issue in Rails 5.2.1

Loading development environment (Rails 5.2.1)
irb(main):001:0> [nil].sum
=> 0

But in ruby 2.5:

$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]

irb(main):001:0> [nil].sum
Traceback (most recent call last):
        4: from /usr/local/bin/irb:11:in `<main>'
        3: from (irb):1
        2: from (irb):1:in `sum'
        1: from (irb):1:in `+'
TypeError (nil can't be coerced into Integer)