0

How do I return Enumerator from my array wrapper without using already existing array iterators?

class MyArray

  def initialize
   @inner = []
  end

  def each
    index = 0
    while index < @inner.size
      yield @inner[index] if block_given?
      index += 1
    end
  end
end

I can't figure out how to avoid calling things like @inner.each at the end of the each method.

2
  • @sawa I fixed it. There was a naming mismatch. Commented Oct 10, 2013 at 13:29
  • 1
    @sawa Ok, there was an extra def before class. Commented Oct 10, 2013 at 13:31

1 Answer 1

1

Given:

@inner = [1, 2, 3]

Code

@inner.to_enum

will return an enumerator.

enum = @inner.to_enum
enum.each{|e| p e}
# => 1, 2, 3
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the solution, code correction and english correction :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.