Skip to content

Instantly share code, notes, and snippets.

@whiz25
Last active May 13, 2020 12:12
Show Gist options
  • Save whiz25/a5c3bbda553256a72d9a8c5be1400021 to your computer and use it in GitHub Desktop.
Save whiz25/a5c3bbda553256a72d9a8c5be1400021 to your computer and use it in GitHub Desktop.
- [ ] should be identical and returns the same thing as ruby's [`each_with_index`](https://ruby-doc.org/core-2.6.4/Enumerable.html#method-i-each_with_index). ([Screenshot](https://gitlab.com/microverse/guides/projects/requirements_screenshots/raw/master/images/ruby/advanced_building_blocks_enumerable/my_each_with_index.png) from [Odin](https://www.theodinproject.com/courses/ruby-programming/lessons/advanced-building-blocks#assignment-2))
- [ ] calls the given block once for each element in self
```ruby
array = Array.new(100){rand(0...9)}
my_each_output = ''
block = proc { |num, idx| my_each_output += "Num: #{num}, idx: #{idx}\n" }
array.each_with_index(&block)
each_output = my_each_output.dup
my_each_output = ''
array.my_each_with_index(&block)
my_each_output == each_output #true
```
- [ ] after calling the given block once for each element in self, returns the array itself
```ruby
my_each_output = ''
block = proc { |num, idx| my_each_output += "Num: #{num}, idx: #{idx}\n" }
array.my_each_with_index(&block) == array.each_with_index(&block) # should return true
```
- [ ] returns an enumerator if no block is given
```ruby
array.my_each_with_index == Enumerator #true
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment