Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make each return an Enumerator object if not given a block to iterate with #41

Open
nat-n opened this issue May 31, 2014 · 1 comment
Open

Comments

@nat-n
Copy link

nat-n commented May 31, 2014

It would be useful if NArray provided a standard ruby Enumerator interface. All that would be required is for my_narray.each to return an Enumerator object, like my_array.each does when called without a block.

This would enable use cases such as my_narray.each.each_with_index { ... } or my_narray.each.map { ... } etc.

The follow monkey patch provides this functionality but I'm sure a better solution is possible.

class NArray
  alias_method :native_each, :each

  def each
    return enum_for :each unless block_given?
    native_each { |item| yield item }
  end
end
@masa16
Copy link
Owner

masa16 commented Jun 2, 2014

Thank you for suggestion. 67e8187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants