Skip to content

Instantly share code, notes, and snippets.

@pocke
Created December 14, 2018 15:14
Show Gist options
  • Select an option

  • Save pocke/fa37768f6db6ae43d59e94e054636f1b to your computer and use it in GitHub Desktop.

Select an option

Save pocke/fa37768f6db6ae43d59e94e054636f1b to your computer and use it in GitHub Desktop.
class Module
def to_enum(method_name)
original_method_name = :"__{method_name}_orig__"
alias_method original_method_name, method_name
define_method(method_name) do |*args, &block|
return enum_for(method_name, *args) unless block
__send__ original_method_name, *args, &block
end
end
end
class A
def initialize(array)
@array = array
end
to_enum def each_with_plus(n)
@array.each do |item|
yield item + n
end
end
end
A.new([1,2,3]).each_with_plus(10) do |n|
p n
end
p(A.new([1,2,3]).each_with_plus(10).map do |n|
n
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment