Note that there are some explanatory texts on larger screens.

plurals
  1. POClean solution to this ruby iterator trickiness?
    text
    copied!<pre><code>k = [1,2,3,4,5] for n in k puts n if n == 2 k.delete(n) end end puts k.join(",") # Result: # 1 # 2 # 4 # 5 # [1,3,4,5] # Desired: # 1 # 2 # 3 # 4 # 5 # [1,3,4,5] </code></pre> <p>This same effect happens with the other array iterator, k.each:</p> <pre><code>k = [1,2,3,4,5] k.each do |n| puts n if n == 2 k.delete(n) end end puts k.join(",") </code></pre> <p>has the same output.</p> <p>The reason this is happening is pretty clear...Ruby doesn't actually iterate through the objects stored in the array, but rather just turns it into a pretty array index iterator, starting at index 0 and each time increasing the index until it's over. But when you delete an item, it still increments the index, so it doesn't evaluate the same index twice, which I want it to.</p> <p>This <em>might</em> not be what's happening, but it's the best I can think of.</p> <p>Is there a clean way to do this? Is there already a built-in iterator that can do this? Or will I have to dirty it up and do an array index iterator, and not increment when the item is deleted? (or iterate through a clone of the array, and delete from the original array)</p> <hr> <p><strong>Clarification</strong></p> <p>I don't simply want to delete items from an array; sorry if that was clear. What I'd want to do is iterate through each element, and "process" it; this process might sometimes delete it. To be more accurate:</p> <pre><code>class Living_Thing def initialize tracker,id @tracker = tracker @id = id @tracker &lt;&lt; self end def process do_stuff puts @id if @id == 2 die end end def die do_stuff_to_die @tracker.delete(self) end def inspect @id end end tracking_array = Array.new() foo = Living_Thing.new(tracking_array,1) bar = Living_Thing.new(tracking_array,2) rab = Living_Thing.new(tracking_array,3) oof = Living_Thing.new(tracking_array,4) puts tracking_array.join(",") # =&gt; [1, 2, 3, 4] for n in tracking_array n.process end # result: only foo, bar, and oof are processed </code></pre> <p>Ideally, I'd want all items in tracking_array to be processed.</p> <p>When Living_Thing is removed from tracking_array, Living_Thing#die <em>must</em> be called; do_stuff_to_die cleans up things that have to be claned up.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload