I'd like to delete an item in my loop i have a array of instance of my class, and i need sometimes to delete this items from my array
class Test
attr_reader :to_del
def initialize(str)
@to_del = str
end
end
tab = Array.new
a = Test.new(false)
b = Test.new(true)
c = Test.new(false)
tab.push(a)
tab.push(b)
tab.push(c)
for l in tab
if l.to_del == true
l = nil
end
end
p tab
any idea ?
tab = [Test.new(false), Test.new(true), Test.new(false)]. As you gain experience with Ruby you'll find thatforis very rarely used. (I've never used it.) That's because it's almost always better to employ a method that enumerates the elements of the receiver (here an array), as has been done by in both answers so far.