0

I am trying to delete all elements from an array. For example:

@array = [1,2,3,4,5]
@array.each do |element|
  @array.delete(element)
end

Now, I understand why the code above does not work. However, I am tasked with deleting all elements of an array while using this delete_entry method:

def delete_entry(entry)
   @array.delete(entry)
end

I have read that removing elements in the midst of iteration is disallowed by design in ruby. Any ideas as to how I would go about deleting all elements from an array while using the delete_entry method in my implementation?

country_dev
  • 605
  • 4
  • 13
  • 23

4 Answers4

3
def delete_entry(entry)
  @array.delete(entry)
end

@array = [1,2,3,4,5]

delete_entry(@array.first) until @array.empty?
@array #=> []

I think this reads better than using an enumerator and requires fewer operations when @array contains duplicate values.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
2

Loop through the array in reverse:

@array = [1,2,3,4,5]
@array.reverse_each do |element|
  @array.delete(element)
end
p @array # => []
steenslag
  • 79,051
  • 16
  • 138
  • 171
  • *Builds a temporary array and traverses that array in reverse order.* – 7stud Jun 14 '15 at 15:38
  • @7stud, [Array#reverse_each](http://ruby-doc.org/core-2.2.0/Array.html#method-i-reverse_each) is an enumerator, so no temporary array is constructed. When given a block, it returns the receiver, just as `Array.each` does. – Cary Swoveland Jun 14 '15 at 16:34
  • @Cary Swoveland, Yeah, I quoted the Enumerable docs--I didn't realize Array had a reverse_each(). – 7stud Jun 14 '15 at 17:02
1
@array = [1,2,3,4,5]
@_array = @array.clone
@_array.each do |element|
  delete_entry(element)
end
Prathan Thananart
  • 4,007
  • 3
  • 19
  • 18
1
class Dog
  attr_reader :array

  def initialize
    @array = [1, 2, 3]
  end

  def delete_entry(entry)
     @array.delete(entry)
  end
end


d = Dog.new

d.array.length.times do 
  d.delete_entry(d.array[0])
end

p d.array

--output:--
[]

I have read that removing elements in the midst of iteration is disallowed by design in ruby.

Ridiculous. It just won't work as you may expect. Here's why:

deleting elements from an array while iterating over the array

Community
  • 1
  • 1
7stud
  • 46,922
  • 14
  • 101
  • 127
  • 2
    What is the point of class creation? your approach as simple as these 2 lines: `@array = [1,2,3,4,5]; @array.size.times { |element| @array.delete(@array[0]) }` – Rustam Gasanov Jun 14 '15 at 15:20
  • @RustamA.Gasanov, What is the point of using an `@variable` at the top level in your code? – 7stud Jun 14 '15 at 15:37
  • in order not to create mysterious `Dog`s and just show the approach which op can apply to his code? If you don't like instance variables on top level what is stopping you from writing `array = [1,2,3,4,5]; array.size.times { |element| array.delete(array[0]) }` I just don't understand how `Dog` is related with question and artificial code complication – Rustam Gasanov Jun 14 '15 at 15:39
  • Do you ask what is the point of writing 2 lines of code instead of 20? :) – Rustam Gasanov Jun 14 '15 at 15:44
  • @Rustam A. Gasanov, ***If you don't like instance variables on top level what is stopping you from writing array = [1,2,3,4,5];*** Because that wasn't the op's code. Your example does the same thing to the main object and the Object class(ignoring private access) that my code does to a dog instance and the Dog class--except that you have exposed a global method to all objects instead of limiting its scope to the Dog class. OKAY?! The op's example seemed to come from inside a class, so I mimicked that class with the Dog class. – 7stud Jun 14 '15 at 16:16