Project

General

Profile

Feature #17608

Updated by sawa (Tsuyoshi Sawada) almost 4 years ago

Many use cases of `Array#sum` are preceded with the `compact` method or followed 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. `array#filter_sum` method 

 ```ruby 
 a.filter_sum # => 6 
 ``` 

 B. An option for `Array#sum`  

 ```ruby 
 a.sum(compact: true) # => 6 
 ```

Back