Skip to content

Instantly share code, notes, and snippets.

@wowremywang
Created October 21, 2021 11:34
Show Gist options
  • Save wowremywang/9d5786a1a33adee7aae1c39e7505d22e to your computer and use it in GitHub Desktop.
Save wowremywang/9d5786a1a33adee7aae1c39e7505d22e to your computer and use it in GitHub Desktop.
Avoid endless loop
# Bad
range = 1..Float::INFINITY
p range.collect { |x| x * x }.first(10)
# Good
range = 1..Float::INFINITY
p range.lazy.collect { |x| x * x }.first(10)
Reference: http://patshaughnessy.net/2013/4/3/ruby-2-0-works-hard-so-you-can-be-lazy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment