Skip to content

Instantly share code, notes, and snippets.

@knu
Created December 19, 2018 08:54
Show Gist options
  • Save knu/4fd422f5ebe6fbbeea8b1d4adcc1ec12 to your computer and use it in GitHub Desktop.
Save knu/4fd422f5ebe6fbbeea8b1d4adcc1ec12 to your computer and use it in GitHub Desktop.
module Foo
refine Hash do
def each_smtn(&block)
return to_enum(__method__) unless block
block.call(1)
block.call(2)
end
end
end
using Foo
Hash.new.each_smtn.each { |x| p x }
#=> undefined method `each_smtn' for {}:Hash (NoMethodError)
@knu
Copy link
Author

knu commented Dec 19, 2018

You can do this instead:

return Enumerator.new { |y| each_smtn { |x| y << x } } unless block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment