[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

pathname's each_entry

Claudio Greco

10/2/2008 7:23:00 PM

Hi all,

I noticed that the following code

require 'pathname'
p = Pathname.new('Test')
p.each_entry { | f | puts f.exist? }

does not work as I would expect.
I'm expecting that any returned object exists otherwise it wouldn't be
returned.
If 'Test' is a folder containing a single file, the iterator returns
three objects, and rightly so: . .. and the file, but only the first two
objects (directories) exist, not the file. Is this the expected
behavior?

Thanks in advance,

Claudio
--
Posted via http://www.ruby-....

6 Answers

Daniel Berger

10/2/2008 7:33:00 PM

0



On Oct 2, 1:22=A0pm, Claudio Greco <claudio_gr...@yahoo.com> wrote:
> Hi all,
>
> I noticed that the following code
>
> require 'pathname'
> p =3D Pathname.new('Test')
> p.each_entry { | f | puts f.exist? }
>
> does not work as I would expect.
> I'm expecting that any returned object exists otherwise it wouldn't be
> returned.

Nope, Pathname.new doesn't enforce that a particular pathname exists
on your filesystem. That's up to you.

Regards,

Dan

Claudio Greco

10/2/2008 8:04:00 PM

0

Daniel Berger wrote:
> Nope, Pathname.new doesn't enforce that a particular pathname exists
> on your filesystem. That's up to you.

Can you please clarify?
If I ask Pathname to return the entries that exist within a folder, then
by definition the objects returned must exist.

Claudio
--
Posted via http://www.ruby-....

Daniel Berger

10/2/2008 9:37:00 PM

0



On Oct 2, 2:03=A0pm, Claudio Greco <claudio_gr...@yahoo.com> wrote:
> Daniel Berger wrote:
> > Nope, Pathname.new doesn't enforce that a particular pathname exists
> > on your filesystem. That's up to you.
>
> Can you please clarify?
> If I ask Pathname to return the entries that exist within a folder, then
> by definition the objects returned must exist.

Hm, you're right. I was thinking each_entry simply yielded each piece
of the path.

I think you're getting false because Pathname#exist? isn't expanding
the path on the test. Trying using it in conjunction with
File.expand_path and see if that works.

Regards,

Dan

Claudio Greco

10/3/2008 6:21:00 AM

0

Daniel Berger wrote:
> Hm, you're right. I was thinking each_entry simply yielded each piece
> of the path.
> ...
> Trying using it in conjunction with
> File.expand_path and see if that works.
>

I tried that before starting this thread

Claudio
--
Posted via http://www.ruby-....

Sean O'Halpin

10/3/2008 7:04:00 AM

0

On Fri, Oct 3, 2008 at 7:20 AM, Claudio Greco <claudio_greco@yahoo.com> wrote:
> Daniel Berger wrote:
>> Hm, you're right. I was thinking each_entry simply yielded each piece
>> of the path.
>> ...
>> Trying using it in conjunction with
>> File.expand_path and see if that works.
>>
>
> I tried that before starting this thread
>
> Claudio
> --

Try this:

require 'pathname'
path = Pathname.new('Test')
path.each_entry { | f |
p [f, f.exist?, f.expand_path, path.join(f), path.join(f).exist?]
}
# >> [#<Pathname:.>, true, #<Pathname:/Users/ohalps01/scratch>,
#<Pathname:Test>, true]
# >> [#<Pathname:..>, true, #<Pathname:/Users/ohalps01>, #<Pathname:.>, true]
# >> [#<Pathname:hello>, false,
#<Pathname:/Users/ohalps01/scratch/hello>, #<Pathname:Test/hello>,
true]

It appears that each_entry yields just the basename, without any context.

Regards,
Sean

Claudio Greco

10/3/2008 7:54:00 AM

0

Sean O'halpin wrote:
> It appears that each_entry yields just the basename, without any
> context.

You're right.

require 'pathname'
p = Pathname.new('C:/') # the same issue happens in Ubuntu
p.each_entry { | x | puts x.expand_path }

This code does not expand the path by prepending C:/, which I think
would be the wanted behavior: it prepends the current working directory.

In order to get my working directory I have to code as follows:

require 'pathname'
p = Pathname.new('C:/')
p.each_entry { | x | puts x.expand_path('C:/') }

So I've had a look at the source code for Pathname and each_entry, and
whenever each_entry iterates over the file system objects (located
within p in my case), the entry x is initialized simply with the name
(in this case the name of the file within C:/) and the link with C:/ is
lost forever, so while p knows about C:/, x does not.

The problem I think is that each_entry is implemented as:

def each_entry(&block)
Dir.foreach(@path) {|f| yield self.class.new(f) }
end

while I would prefer the following:

def each_entry(&block)
Dir.foreach(@path) {|f| yield self.class.new(@path+f) }
end



Thanks,

Claudio
--
Posted via http://www.ruby-....