# Pathname's each\_entry

**URL:** <https://www.ruby-forum.com/t/pathnames-each-entry/150046>\
**Category:** Ruby\
**Created:** [October 2, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/pathnames-each-entry/150046 "2008-10-02T21:23:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![claudio\_greco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@claudio\_greco](https://www.ruby-forum.com/u/claudio_greco)\
**Post date:** [October 2, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/pathnames-each-entry/150046/1 "2008-10-02T21:23:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![claudio\_greco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@claudio\_greco](https://www.ruby-forum.com/u/claudio_greco)\
**Post date:** [October 2, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/pathnames-each-entry/150046/2 "2008-10-02T21:34:53Z")

</div>

On Oct 2, 1:22 pm, Claudio G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![claudio\_greco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@claudio\_greco](https://www.ruby-forum.com/u/claudio_greco)\
**Post date:** [October 2, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/pathnames-each-entry/150046/3 "2008-10-02T22:05:11Z")

</div>

Daniel B. 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

---

<div class="post-metadata">

**Author:** ![claudio\_greco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@claudio\_greco](https://www.ruby-forum.com/u/claudio_greco)\
**Post date:** [October 3, 2008, 8:22am UTC](https://www.ruby-forum.com/t/pathnames-each-entry/150046/4 "2008-10-03T08:22:11Z")

</div>

Daniel B. 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

---

<div class="post-metadata">

**Author:** ![claudio\_greco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@claudio\_greco](https://www.ruby-forum.com/u/claudio_greco)\
**Post date:** [October 2, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/pathnames-each-entry/150046/5 "2008-10-02T23:40:31Z")

</div>

On Oct 2, 2:03 pm, Claudio G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Daniel B. 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

---

<div class="post-metadata">

**Author:** ![claudio\_greco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@claudio\_greco](https://www.ruby-forum.com/u/claudio_greco)\
**Post date:** [October 3, 2008, 9:06am UTC](https://www.ruby-forum.com/t/pathnames-each-entry/150046/6 "2008-10-03T09:06:49Z")

</div>

On Fri, Oct 3, 2008 at 7:20 AM, Claudio G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## 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

---

<div class="post-metadata">

**Author:** ![claudio\_greco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/77aa72/32.png) [@claudio\_greco](https://www.ruby-forum.com/u/claudio_greco)\
**Post date:** [October 3, 2008, 9:55am UTC](https://www.ruby-forum.com/t/pathnames-each-entry/150046/7 "2008-10-03T09:55:20Z")

</div>

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
