Skip to content

Instantly share code, notes, and snippets.

@jcwilk
Created May 7, 2022 18:25
Show Gist options
  • Save jcwilk/2a03147d2278dcc3283cbb2d466231f5 to your computer and use it in GitHub Desktop.
Save jcwilk/2a03147d2278dcc3283cbb2d466231f5 to your computer and use it in GitHub Desktop.
Example of how lazy enumerables break when you try to refine methods into them
module LockedEnumeration
refine Enumerator::Lazy do
def refined
if block_given?
each { |el| yield el }
else
enum_for(:refined)
end
end
end
end
using LockedEnumeration
Enumerator.new {}.lazy.refined # => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator: #<Enumerator::Generator:0x0000555ee6935428>:each>>:refined>
Enumerator.new {}.lazy.refined.to_a # => undefined method `refined' for #<Enumerator::Lazy:0x0000555ee692fc08> (NoMethodError)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment