On 15.10.2007 20:38, Konrad Meyer wrote:
> Quoth Robert Klemme:
>> On 15.10.2007 19:41, Simon Schuster wrote:
>>> it seems that I have to run it a couple different times for it to
>>> work.. array.length = 16576, if that could be a potential problem. if
>>> it is, how do I work around it?
>>>
>>> 077:0> array = File.read("/text.txt").to_a.each { |x| x.chomp! }
>>> 080:0> array.each_with_index { |x,y|
>>>     array.delete_at(y) if x.empty? == true
>>> }
>>>
>>> still leaves me with plenty of empty array items
>>> 080:0> array[-3]
>>> ""
>> Your bug is in the loop: you iterate and delete at the same time which 
>> will likely yield strange results (as you observe).  Why don't you just do
>>
>> array.delete_if {|x| x.empty?}
>>
>> Btw, empty? == true is quite dangerous because in Ruby "true" is not the 
>> only valid value for true.
> 
> But !!e.empty? == true will do the trick :D. 

I've seen the smiley but, frankly, I find this only moderately funny. 
The reason is that there are too many people around that think comparing 
boolean values is a great idea.  This is at least superfluous and often 
outright dangerous.  Other people then will have to debug those bugs...

Cheers

	robert