Skip to content

Instantly share code, notes, and snippets.

@whiz25
Last active May 6, 2020 16:29
Show Gist options
  • Save whiz25/9f15aab683fc1c78fe6257b7f49f63bd to your computer and use it in GitHub Desktop.
Save whiz25/9f15aab683fc1c78fe6257b7f49f63bd to your computer and use it in GitHub Desktop.
- [ ] should be identical and returns the same thing as ruby's [`inject`](https://ruby-doc.org/core-2.6.4/Enumerable.html#method-i-inject). ([Screenshot](https://gitlab.com/microverse/guides/projects/requirements_screenshots/raw/master/images/ruby/advanced_building_blocks_enumerable/my_inject.png) from [Odin](https://www.theodinproject.com/courses/ruby-programming/lessons/advanced-building-blocks#assignment-2))
- [ ] when a symbol is specified combines each element of the collection by applying the symbol as a named method
```ruby
array = Array.new(100) { rand(0...9) }
array.my_inject(:+) == array.inject(:+) # should true
```
- [ ] combines all elements of enum by applying a binary operation, specified by a block
```ruby
operation = proc { |sum, n| sum + n }
search = proc { |memo, word| memo.length > word.length ? memo : word }
array = Array.new(100){rand(0...9)}
words = %w[dog door rod blade]
array.my_inject(&operation) == array.inject(&operation) # should true
actual = range.my_inject(4) { |prod, n| prod * n }
expected = range.inject(4) { |prod, n| prod * n }
actual == expected # should true
words.my_inject(&search) == words.inject(&search) # should true
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment