Can soneone tell me what I'm doing wrong

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 :slight_smile:

Jeremy Gregorio
gunvalk@cox.net

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 gunvalk@cox.net 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 :slight_smile:

Jeremy Gregorio
gunvalk@cox.net

Dir is more powerful than that:

Dir.chdir ARGV[0]

puts Dir.glob(‘*.mp3’).join(“\n”)

···

Jeremy Gregorio (gunvalk@cox.net) 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” }


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

And for those who haven’t used C++ … what is the issue? And does it
explain why the simpler method I was planning to suggest doesn’t work
either?

My suggestion was going to be:

fileNames = mp3Dir.entries.collect {|f| f =~ /.*mp3$/}

or something much like it. But no go.

···

On Thu, Oct 03, 2002 at 07:44:12AM +0900, William Djaja Tjokroaminata wrote:

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.


Matt Gushee
Englewood, Colorado, USA
mgushee@havenrock.com

Oops! Never mind. ‘collect’ was the wrong method. This works:

fileNames = mp3Dir.entries.find_all {|f| f =~ /.*mp3$/}

···

On Thu, Oct 03, 2002 at 07:50:02AM +0900, Matt Gushee wrote:

On Thu, Oct 03, 2002 at 07:44:12AM +0900, William Djaja Tjokroaminata wrote:

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.

And for those who haven’t used C++ … what is the issue? And does it
explain why the simpler method I was planning to suggest doesn’t work
either?

My suggestion was going to be:

fileNames = mp3Dir.entries.collect {|f| f =~ /.*mp3$/}

or something much like it. But no go.


Matt Gushee
Englewood, Colorado, USA
mgushee@havenrock.com

Cool, thanks. I haven’t programmed ruby (or any OO language) in a
while, so I forgot about that little bit. Thanks again.

Jeremy Gregorio
gunvalk@cox.net

Well, I don’t know how to explain it easily without using the concept of
pointer. For the expert Ruby programmers, can someone help here?

Regards,

Bill

···

===========================================================================
Matt Gushee mgushee@havenrock.com wrote:

On Thu, Oct 03, 2002 at 07:50:02AM +0900, Matt Gushee wrote:

On Thu, Oct 03, 2002 at 07:44:12AM +0900, William Djaja Tjokroaminata wrote:

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.

And for those who haven’t used C++ … what is the issue?

Well, I do have a general idea of what pointers are, though I can’t
speak for other non-C++ programmers. Can’t promise to understand, but
you might try your pointerful explanation.

···

On Thu, Oct 03, 2002 at 08:44:26AM +0900, William Djaja Tjokroaminata wrote:

Well, I don’t know how to explain it easily without using the concept of
pointer. For the expert Ruby programmers, can someone help here?


Matt Gushee
Englewood, Colorado, USA
mgushee@havenrock.com

Hello William,

Thursday, October 03, 2002, 3:44:26 AM, you wrote:

use a concept of index :slight_smile: when we delete element currently indexed by
iterator, this index will point to next element and when we increment
that index, we will just skip one element totally

···

Well, I don’t know how to explain it easily without using the concept of
pointer. For the expert Ruby programmers, can someone help here?

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.


Best regards,
Bulat mailto:bulatz@integ.ru

Well, that’s simple enough. I was expecting something more esoteric. So
does this mean that at the implementation level there’s really no
difference between

stuff.each

and

for (int i = 0; i < stuff.length(); i++) {

?

···

On Thu, Oct 03, 2002 at 02:21:32PM +0900, Bulat Ziganshin wrote:

use a concept of index :slight_smile: when we delete element currently indexed by
iterator, this index will point to next element and when we increment
that index, we will just skip one element totally

Well, I don’t know how to explain it easily without using the concept of
pointer. For the expert Ruby programmers, can someone help here?

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.


Matt Gushee
Englewood, Colorado, USA
mgushee@havenrock.com

Hi Bulat,

See,

... this index will *point* to next element ...

There I think we use a concept of pointer :). Fortunately, Matt grasped
it easily.

Regards,

Bill

···

===========================================================================
Bulat Ziganshin bulatz@integ.ru wrote:

Hello William,

Thursday, October 03, 2002, 3:44:26 AM, you wrote:

use a concept of index :slight_smile: when we delete element currently indexed by
iterator, this index will point to next element and when we increment
that index, we will just skip one element totally

Well, I don’t know how to explain it easily without using the concept of
pointer. For the expert Ruby programmers, can someone help here?

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.


Best regards,
Bulat mailto:bulatz@integ.ru

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.

Well, that’s simple enough. I was expecting something more esoteric. So
does this mean that at the implementation level there’s really no
difference between

stuff.each

and

for (int i = 0; i < stuff.length(); i++) {

?


Matt Gushee

For an array, perhaps, but it’s not true for a generic iterator. Not
everything that supports :each also supports :length.

Or : for that matter.

Gavin

···

From: “Matt Gushee” mgushee@havenrock.com

Hello Matt,

Thursday, October 03, 2002, 9:44:54 AM, you wrote:

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.

Well, that’s simple enough. I was expecting something more esoteric. So
does this mean that at the implementation level there’s really no
difference between

stuff.each

and

for (int i = 0; i < stuff.length(); i++) {

definitely yes! iterator is a method of ABSTRACTING this cycle, hiding
implementation details from class user. but internally it runs the
same cycle

···


Best regards,
Bulat mailto:bulatz@integ.ru

Hi,

Both Java and C++ have iterators, but usually in C++ books the iterators
are accompanied by examples on how to write them in C, and therefore it is
crystal clear there the issues that may surface.

Regards,

Bill

···

=======================================================================
Bulat Ziganshin bulatz@integ.ru wrote:

Well, that’s simple enough. I was expecting something more esoteric. So
does this mean that at the implementation level there’s really no
difference between

stuff.each

and

for (int i = 0; i < stuff.length(); i++) {

definitely yes! iterator is a method of ABSTRACTING this cycle, hiding
implementation details from class user. but internally it runs the
same cycle

Hello William,

Thursday, October 03, 2002, 5:25:42 PM, you wrote:

in ruby books there is the same examples. may be difference is that
the ruby programmers may not read this examples in order to start
using iterators :slight_smile:

···

Both Java and C++ have iterators, but usually in C++ books the iterators
are accompanied by examples on how to write them in C, and therefore it is
crystal clear there the issues that may surface.

Well, that’s simple enough. I was expecting something more esoteric. So
does this mean that at the implementation level there’s really no
difference between

stuff.each

and

for (int i = 0; i < stuff.length(); i++) {


Best regards,
Bulat mailto:bulatz@integ.ru