J
Jari Williamsson
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
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