Skip to content

Instantly share code, notes, and snippets.

@georgi
Created February 5, 2015 17:18
Show Gist options
  • Save georgi/2c5c11998a3acd2a632d to your computer and use it in GitHub Desktop.
Save georgi/2c5c11998a3acd2a632d to your computer and use it in GitHub Desktop.
Enumerators and Enumerables
# Iterator methods without blocks are called Enumerators
[1,2,3].each_index # => #<Enumerator: [1, 2, 3]:each_index>
0.upto(10) # => #<Enumerator: 0:upto(10)>
# You can call any Enumerable method on Enumerators
[1,2,3].each_index.map {|i| i + 10 } # => [10, 11, 12]
0.upto(10).map {|i| i + 10 } # => [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# See documentation about Enumerables
# http://ruby-doc.org/core-2.1.5/Enumerable.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment