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?

3
  • Array#delete_if? Commented Jun 14, 2015 at 14:46
  • That was my initial thought: @array.delete_if { true }. But I am required to use the delete_entry method. Commented Jun 14, 2015 at 14:48
  • 1
    I think it would be more accurate to say, "removing elements of a receiver while enumerating over them is dicouraged..." Commented Jun 14, 2015 at 17:02

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

[nil, nil, false, false]?
2

Loop through the array in reverse:

@array = [1,2,3,4,5]
@array.reverse_each do |element|
  @array.delete(element)
end
p @array # => []

3 Comments

Builds a temporary array and traverses that array in reverse order.
@7stud, Array#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, Yeah, I quoted the Enumerable docs--I didn't realize Array had a reverse_each().
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

5 Comments

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]) }
@RustamA.Gasanov, What is the point of using an @variable at the top level in your code?
in order not to create mysterious Dogs 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
Do you ask what is the point of writing 2 lines of code instead of 20? :)
@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.
1
@array = [1,2,3,4,5]
@_array = @array.clone
@_array.each do |element|
  delete_entry(element)
end

Comments

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.