Skip to content

Instantly share code, notes, and snippets.

@stevo
Last active July 27, 2018 10:34
Show Gist options
  • Save stevo/bc457cac6a5832109e4e387853662f48 to your computer and use it in GitHub Desktop.
Save stevo/bc457cac6a5832109e4e387853662f48 to your computer and use it in GitHub Desktop.
def with_enumerated(subject, method_name)
begin
subject.define_singleton_method("#{method_name}_with_enum") do |*args, &block|
return enum_for(method_name, *args) unless block.present?
public_send("#{method_name}_without_enum",*args, &block)
end
subject.singleton_class.alias_method "#{method_name}_without_enum", method_name
subject.singleton_class.alias_method method_name, "#{method_name}_with_enum"
yield
ensure
subject.singleton_class.alias_method method_name, "#{method_name}_without_enum"
subject.singleton_class.remove_method "#{method_name}_with_enum"
subject.singleton_class.remove_method "#{method_name}_without_enum"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment