Dir.foreach not with patterns?

Why does the third one not work as expected? It appears that
Dir.foreach will not work with patterns…

#!/usr/bin/ruby

s = “/etc/host*”

#1
l = Dir[s]
p l

#2
Dir.foreach("/etc") { |d| p d }

#3
Dir.foreach(s) { |d| p d }

#4

This is wasteful

l = Dir[s]
l.each { |f| p f }

···


^^^ Kurt

There is no good nor evil, just power.

Hi,

···

At Sat, 13 Sep 2003 02:28:02 +0900, Kurt V. Hindenburg wrote:

Why does the third one not work as expected? It appears that
Dir.foreach will not work with patterns…

Use Dir.glob.


Nobu Nakada

Hi –

···

On Sat, 13 Sep 2003, Kurt V. Hindenburg wrote:

Why does the third one not work as expected? It appears that
Dir.foreach will not work with patterns…

#!/usr/bin/ruby

s = “/etc/host*”

#1
l = Dir[s]
p l

#2
Dir.foreach(“/etc”) { |d| p d }

#3
Dir.foreach(s) { |d| p d }

#4

This is wasteful

l = Dir[s]
l.each { |f| p f }

You don’t need two lines, though – you can do this in a way that’s actually
shorter than #3:

Dir[s].each {|f| p f}

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav