Skip to content

Instantly share code, notes, and snippets.

@jgaskins
Created February 8, 2013 15:41
Show Gist options
  • Save jgaskins/4739788 to your computer and use it in GitHub Desktop.
Save jgaskins/4739788 to your computer and use it in GitHub Desktop.
Clojure's `iterate` function in Ruby
class Object
def iterate(&block)
Enumerator.new do |yielder|
current = self
loop do
yielder << current
current = yield(current)
end
end
end
end
p 1.iterate{|i| i + 1}.take(5)
# => [1, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment