fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

[Enhance] Add Another Inject-Like Method That Might Occur in Ruby for Beginners #144

Closed berniechiu closed 4 years ago

berniechiu commented 6 years ago

Objective

Since some Ruby beginners still write aggregations without inject, it's good to show them a benchmark on different usage that might occur to them.

Feature

berniechiu commented 6 years ago

Wonder why some of the approved PRs not merging lol ~

ixti commented 6 years ago

There are at least 2 more ways of doing the same:

arr.sum # available as of Ruby 2.4
arr.inject(:+)

Also when you do summing you better pass initial memo:

arr = []

arr.sum # => 0
arr.inject(:+) # => nil
arr.inject(0, :+) # => 0
arr.inject(&:+) # => nil
arr.inject(0, &:+) # => 0
ixti commented 6 years ago

TBH IMO all that part should be completely rewritten. There's no point to refer rubies 1.x - they are EOL long time ago.

FunkyloverOne commented 4 years ago

OMG, 30x+ difference :o