Delete array element within iterator block?

How do I (in an elegant way) delete an element of an array within a block, while using a block iterator?

The code below won't work (the wanted behavior here is to get all array elements to print and but also delete the 2nd element after it has been encountered):

···

---
arr = [1,2,3,4]
arr.each_with_index { |e, i|
   p e
   arr.delete_at(i) if i == 1
}
---
results in:
1
2
4

Of curse, I could loop and keep track of the index manually, but I'm mainly checking if there is any built-in stuff here.

Best regards,

Jari Williamsson

How do I (in an elegant way) delete an element of an array within a block, while using a block iterator?

I assume using delete_if is no good in this situation?
[1,2,3,4,5,6].delete_if{|i| i>4}
> [1,2,3,4]

The code below won't work (the wanted behavior here is to get all array elements to print and but also delete the 2nd element after it has been encountered):
---
arr = [1,2,3,4]
arr.each_with_index { |e, i|
p e
arr.delete_at(i) if i == 1
}
---
results in:
1
2
4

This is interesting, because although the results printed on the screen imply the 3 was removed, if you look at the array afterthe operation you'll see it was the 2 removed (as we wanted)

irb(main):013:0> (a=[1,2,3,4]).each_with_index{|e,i| p e;a.delete_at(i) if i==1}
1
2
4
=> [1, 3, 4]

I've no ideas why

Cheers,
Dave

···

On 30/11/2007, at 10:51 PM, Jari Williamsson wrote:

Of curse, I could loop and keep track of the index manually, but I'm mainly checking if there is any built-in stuff here.

Best regards,

Jari Williamsson

HI --

How do I (in an elegant way) delete an element of an array within a block, while using a block iterator?

I assume using delete_if is no good in this situation?
[1,2,3,4,5,6].delete_if{|i| i>4}

[1,2,3,4]

The code below won't work (the wanted behavior here is to get all array elements to print and but also delete the 2nd element after it has been encountered):
---
arr = [1,2,3,4]
arr.each_with_index { |e, i|
p e
arr.delete_at(i) if i == 1
}
---
results in:
1
2
4

This is interesting, because although the results printed on the screen imply the 3 was removed, if you look at the array afterthe operation you'll see it was the 2 removed (as we wanted)

irb(main):013:0> (a=[1,2,3,4]).each_with_index{|e,i| p e;a.delete_at(i) if i==1}
1
2
4
=> [1, 3, 4]

I've no ideas why

First time through:

   a => [1,2,3,4]
   e => 1
   i => 0

Second time through:

   a => [1,2,3,4]
   e => 2
   i => 1

Third time through:

   a => [1,3,4]
   e => 4 # i.e., a[2]
   i => 2

Or some such thing. Of course the best idea is never to do this :slight_smile:

David

···

On Fri, 30 Nov 2007, Sharon Phillips wrote:

On 30/11/2007, at 10:51 PM, Jari Williamsson wrote:

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
    * Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details and 2008 announcements!

Sharon Phillips wrote:

···

On 30/11/2007, at 10:51 PM, Jari Williamsson wrote:

How do I (in an elegant way) delete an element of an array within a block, while using a block iterator?

I assume using delete_if is no good in this situation?
[1,2,3,4,5,6].delete_if{|i| i>4}
> [1,2,3,4]

Thanks, delete_if works just fine!

Best regards,

Jari Williamsson