Skip to content

Commit

Permalink
Merge pull request #49214 from akhilgkrishnan/enumerables-sum-doc
Browse files Browse the repository at this point in the history
Remove Enumerable#sum from guide [skip ci]
  • Loading branch information
p8 committed Sep 10, 2023
2 parents ed0c34d + a564e75 commit b1ac22f
Showing 1 changed file with 0 additions and 41 deletions.
41 changes: 0 additions & 41 deletions guides/source/active_support_core_extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2197,47 +2197,6 @@ BigDecimal(5.00, 6).to_s("e") # => "0.5E1"
Extensions to `Enumerable`
--------------------------

### `sum`

The method [`sum`][Enumerable#sum] adds the elements of an enumerable:

```ruby
[1, 2, 3].sum # => 6
(1..100).sum # => 5050
```

Addition only assumes the elements respond to `+`:

```ruby
[[1, 2], [2, 3], [3, 4]].sum # => [1, 2, 2, 3, 3, 4]
%w(foo bar baz).sum # => "foobarbaz"
{ a: 1, b: 2, c: 3 }.sum # => [:a, 1, :b, 2, :c, 3]
```

The sum of an empty collection is zero by default, but this is customizable:

```ruby
[].sum # => 0
[].sum(1) # => 1
```

If a block is given, `sum` becomes an iterator that yields the elements of the collection and sums the returned values:

```ruby
(1..5).sum { |n| n * 2 } # => 30
[2, 4, 6, 8, 10].sum # => 30
```

The sum of an empty receiver can be customized in this form as well:

```ruby
[].sum(1) { |n| n**3 } # => 1
```

NOTE: Defined in `active_support/core_ext/enumerable.rb`.

[Enumerable#sum]: https://api.rubyonrails.org/classes/Enumerable.html#method-i-sum

### `index_by`

The method [`index_by`][Enumerable#index_by] generates a hash with the elements of an enumerable indexed by some key.
Expand Down

0 comments on commit b1ac22f

Please sign in to comment.