[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dir.mkdir erases

gray

4/17/2006 1:51:00 AM

Hi, I'm just starting out with Ruby, but I noticed some behavior that
seemed counter-intuitive. I was hoping that someone could confirm this
behavior and help explain to me how I need to change my understanding
of the language.

The following code creates an array of filenames and stores them in
'directory'. When I attempt to create a new directory in the
filesystem using Dir, however, the 'directory' array is cleared. In
other words, these instructions print the directory contents only once:

directory = Dir.new(path)
Dir.chdir(path)

# prints the names of all files in path
directory.each { |x| print x + "\n" }

# make a new dir
Dir.mkdir 'foo'

# ?? where did the files go? This prints nothing...
directory.each { |x| print x + "\n" }

The way I understand it, the Dir.new() function creates an array of the
filenames in [path] and stores the reference in [directory]. Why does
the Dir object then clear this array when performing a simple mkdir?

Please keep in mind that my primary concern is understanding the
language, not just to get a piece of code that works.

I appreciate the help! ^^

-gray

1 Answer

David Sletten

4/17/2006 3:02:00 AM

0

gray wrote:

> Hi, I'm just starting out with Ruby, but I noticed some behavior that
> seemed counter-intuitive. I was hoping that someone could confirm this
> behavior and help explain to me how I need to change my understanding
> of the language.
>
> The following code creates an array of filenames and stores them in
> 'directory'. When I attempt to create a new directory in the
> filesystem using Dir, however, the 'directory' array is cleared. In
> other words, these instructions print the directory contents only once:
>
> directory = Dir.new(path)
> Dir.chdir(path)
>
> # prints the names of all files in path
> directory.each { |x| print x + "\n" }
>
> # make a new dir
> Dir.mkdir 'foo'
>
> # ?? where did the files go? This prints nothing...
> directory.each { |x| print x + "\n" }
>
> The way I understand it, the Dir.new() function creates an array of the
> filenames in [path] and stores the reference in [directory]. Why does
> the Dir object then clear this array when performing a simple mkdir?
>
> Please keep in mind that my primary concern is understanding the
> language, not just to get a piece of code that works.
>
> I appreciate the help! ^^
>
> -gray
>

Unfortunately you misunderstood something and then got confused by an
extraneous issue. First, Dir.new does not create an array. Rather it
creates a Dir object. This is a directory stream that can only be read once.

You can ask a Dir object what it is:
irb(main):542:0> Dir.new(".").class
=> Dir

Now try this:
irb(main):538:0> directory = Dir.new(".")
=> #<Dir:0x6871c>
irb(main):539:0> directory.each {|dir| puts(dir)}
.
...
etc...
irb(main):540:0> directory.each {|dir| puts(dir)}
=> #<Dir:0x6871c>

You see the second time, the stream has already been exhausted. So the
loss of your filenames has nothing to do with Dir.mkdir.

If you want a persistent array of filenames in a directory try
Dir.entries(path).

Aloha,
David Sletten