From: William Djaja Tjokroaminata Date: 2002-10-03T07:44:12+09:00 Subject: Re: Can soneone tell me what I'm doing wrong... Hi Jeremy, If you ever used C++, then you would have known the issue of deleting an element while you are traversing the container using an iterator. To make things straightforward, however, you should have used "delete_if" instead of using "each" and "delete": mp3DirName = ARGV[0] mp3Dir = Dir.new(mp3DirName) $fileNames = mp3Dir.entries #this filters out things that aren't .mp3s $fileNames.delete_if { |x| x !~ /.*mp3$/} # this is modified $fileNames.each { | fileName | print fileName + "\n" } Regards, Bill ============================================================================ Jeremy Gregorio wrote: > I'm trying to write a little script to rename my mp3s I started with this: > mp3DirName = ARGV[0] > mp3Dir = Dir.new(mp3DirName) > $fileNames = mp3Dir.entries > #this filters out things that aren't .mp3s > $fileNames.each { |x| if !(x =~ /.*mp3$/) > > $fileNames.delete(x) > > end > > }#end each block > $fileNames.each { | fileName | print fileName + "\n" } > which should print the names of mp3s found the the directory given at > the command line. As near as I can tell the regex test is working right, > but $fileNames.delete(x) doesn't seem to be doing anything. I can do > $fileNames.delete("."); and that removes the entry "." from the list. I > haven't a clue why this doesn't just work, but I'm guessing/hoping it's > some idiosyncracy of ruby that I just don't know about. Thanks again guys :) > Jeremy Gregorio > gunvalk@cox.net