0

Yes I know Ruby doesn't have pointers, but that's the closest I could get to describing what I want to do. In C++, I can increment a pointer to an array and it will point to the second element of the array as the start (basically like splicing from position 1 to size-1). Is there a similar trick in ruby? I have a set of steps in an array, and want to call a method "next_step" that will move one down the array. Can I do this without necessarily having another ivar called step_num (the current index)?

EDIT: I can't just splice the array because it is encapsulated in an object and I need to keep it that way. I don't want copies floating around.

1 Answer 1

3

You can use an enumerator:

a = [ 1, 2, 3 ]
e = a.to_enum

e.next    # => 1
e.next    # => 2
e.next    # => 3
Sign up to request clarification or add additional context in comments.

2 Comments

What does that do to nested objects? The things in the array are objects that themselves contain objects. Will those be flattened or will it just cycle through them?
It does return objects, so this is exactly what I needed. Thanks!

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.