From: sawadatsuyoshi@... Date: 2021-02-05T14:25:46+00:00 Subject: [ruby-core:102402] [Ruby master Feature#17608] Compact and sum in one step Issue #17608 has been updated by sawa (Tsuyoshi Sawada). Hanmac (Hans Mackowiak) wrote in #note-6: > [Y]ou can use [the fact] that `nil.to_i` returns 0 > > `a.sum(&:to_i) #=> 6` That won't work. ```ruby [1.2, nil, 3.6].sum{_1 || 0} # => 4.8 [1.2, nil, 3.6].sum(&:to_i) # => 4 ``` ---------------------------------------- Feature #17608: Compact and sum in one step https://bugs.ruby-lang.org/issues/17608#change-90273 * Author: sawa (Tsuyoshi Sawada) * Status: Open * Priority: Normal ---------------------------------------- Many use cases of `Array#sum` are preceded with the `compact` method or are followed by a block to ensure the value is addable. ```ruby a = [1, nil, 2, 3] a.sum # !> TypeError a.compact.sum # => 6 a.sum{_1 || 0} # => 6 ``` I propose there should be a way to do that in one step. I request either of the following: A. Change the current behaviour to skip `nil`s. ```ruby a.sum # => 6 ``` B. `Array#filter_sum` method ```ruby a.filter_sum # => 6 ``` C. An option for `Array#sum` ```ruby a.sum(compact: true) # => 6 ``` -- https://bugs.ruby-lang.org/ Unsubscribe: