Skip to content

Instantly share code, notes, and snippets.

@banister
Created September 25, 2010 07:27
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/596582 to your computer and use it in GitHub Desktop.
Save banister/596582 to your computer and use it in GitHub Desktop.
# Ruby iteration
range = (1..100)
# internal iteration
range.each { |i| puts i } #=> 1, 2, 3, 4, ..
# external iteration
it = range.to_enum
it.next #=> 1
it.next #=> 2
# generator object (lazily generating values)
g = Enumerator.new { |y|
v = 0
while true
y.yield v += 2
end
}
g.next #=> 2
g.next #=> 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment