Skip to content

Instantly share code, notes, and snippets.

@banister
Created June 2, 2012 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banister/03d992a948928ea074df to your computer and use it in GitHub Desktop.
Save banister/03d992a948928ea074df to your computer and use it in GitHub Desktop.
e = Enumerator.new { |y| (1..10).each { |v| y << v } }
# => #<Enumerator: ...>
e.count
# => 10
e.group_by { |v| v.even? }
# => {false=>[1, 3, 5, 7, 9], true=>[2, 4, 6, 8, 10]}
e.take_while { |v| v < 5 }
# => [1, 2, 3, 4]
e.to_a
# => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
e.rewine
# => #<NoMethodError: undefined method `rewine' for #<Enumerator: #<Enumerator::Generator:0x0000010127d380>:each>>
e.rewind
# => #<Enumerator: ...>
loop { puts e.next }
# => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment