# Why does File.exists? not support regexp? It would be very handfull?

**URL:** <https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376>\
**Category:** ruby-talk\
**Created:** [4 September 2007 15:45 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376 "2007-09-04T15:45:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anansi](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anansi](https://rubytalk.org/u/anansi)\
**Post date:** [4 September 2007 15:45 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376/1 "2007-09-04T15:45:04Z")

</div>

Hi,  
if I wanna check if a file exists like fglrx.i386.8.40.3.ko why is File.exists? not able to handle regexp to check this like:

system('rm fglrx\*') if File.exists?(/^fglrx.+/)

--\> ./test.rb:4:in `exists?': can't convert Regexp into String (TypeError)

> **···**
>
> --  
> kazaam \<[kazaam@oleco.net](mailto:kazaam@oleco.net)\>

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [4 September 2007 15:52 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376/2 "2007-09-04T15:52:57Z")

</div>

Dir["fglrx\*"].each{ |file| File.delete(file) }

Regards,

Dan

> **···**
>
> On Sep 4, 9:45 am, kazaam \<kaz...@oleco.net\> wrote:
> 
> > Hi,  
> > if I wanna check if a file exists like fglrx.i386.8.40.3.ko why is File.exists? not able to handle regexp to check this like:
> > 
> > system('rm fglrx\*') if File.exists?(/^fglrx.+/)
> > 
> > --\> ./test.rb:4:in `exists?': can't convert Regexp into String (TypeError)

---

<div class="post-metadata">

**Author:** ![rio4ruby](https://avatars.discourse-cdn.com/v4/letter/r/82dd89/32.png) [@rio4ruby](https://rubytalk.org/u/rio4ruby)\
**Post date:** [4 September 2007 22:05 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376/3 "2007-09-04T22:05:07Z")

</div>

I agree that file matching via regular expressions is very convenient  
in certain situations that are not handled by globs. You might want to  
look at Rio ([http://rio.rubyforge.org](http://rio.rubyforge.org)).

> **···**
>
> On Sep 4, 8:43 am, kazaam \<kaz...@oleco.net\> wrote:
> 
> > Hi,  
> > if I wanna check if a file exists like fglrx.i386.8.40.3.ko why is File.exists? not able to handle regexp to check this like:
> > 
> > system('rm fglrx\*') if File.exists?(/^fglrx.+/)
> > 
> > --\> ./test.rb:4:in `exists?': can't convert Regexp into String (TypeError)
> > 
> > --  
> > kazaam \<kaz...@oleco.net\>

---

<div class="post-metadata">

**Author:** ![anansi](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anansi](https://rubytalk.org/u/anansi)\
**Post date:** [4 September 2007 16:00 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376/4 "2007-09-04T16:00:05Z")

</div>

that's pretty much the same like: system('rm -f fglrx\*') but I'm asking why one doesn't allow regexpr into File.exists?

> **···**
>
> On Wed, 5 Sep 2007 00:52:57 +0900 Daniel Berger \<djberg96@gmail.com\> wrote:
> 
> > Dir["fglrx\*"].each{ |file| File.delete(file) }
> 
> --  
> kazaam \<kazaam@oleco.net\>

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [4 September 2007 16:27 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376/5 "2007-09-04T16:27:10Z")

</div>

> \> Dir["fglrx\*"].each{ |file| File.delete(file) }
> 
> that's pretty much the same like: system('rm -f fglrx\*') but I'm asking why one doesn't allow regexpr into File.exists?

Probably because most people wouldn't find it intuitive, there's  
already easy alternatives, and there doesn't seem to be any desire for  
it among the Ruby community in general. You're the first person to  
request it that I've seen.

> From a development point of view, it's not only more maintenance, but

also a slipperly slope towards having to review every other boolean  
File method.

Regards,

Dan

> **···**
>
> On Sep 4, 10:00 am, kazaam \<kaz...@oleco.net\> wrote:
> 
> > On Wed, 5 Sep 2007 00:52:57 +0900 \> \> Daniel Berger \<djber...@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [4 September 2007 21:29 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376/6 "2007-09-04T21:29:46Z")

</div>

I would guess it is because file name pattern matching  
is subtly different from string pattern matching. In  
particular you have the problem of directory hierarchies  
and how to handle the separator character (typically '/').  
The common file pattern syntax is also \*not\* the same  
as general regular expressions.

Gary Wright

> **···**
>
> On Sep 4, 2007, at 12:00 PM, kazaam wrote:
> 
> > On Wed, 5 Sep 2007 00:52:57 +0900 \> Daniel Berger \<djberg96@gmail.com\> wrote:
> > 
> > > Dir["fglrx\*"].each{ |file| File.delete(file) }
> > 
> > that's pretty much the same like: system('rm -f fglrx\*') but I'm asking why one doesn't allow regexpr into File.exists?

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [4 September 2007 21:47 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376/7 "2007-09-04T21:47:59Z")

</div>

Gary Wright wrote:

> > > Dir["fglrx\*"].each{ |file| File.delete(file) }
> > 
> > that's pretty much the same like: system('rm -f fglrx\*') but I'm asking why one doesn't allow regexpr into File.exists?
> 
> I would guess it is because file name pattern matching  
> is subtly different from string pattern matching. In  
> particular you have the problem of directory hierarchies  
> and how to handle the separator character (typically '/').  
> The common file pattern syntax is also \*not\* the same  
> as general regular expressions.

The slocate(1) program does have a -r option for searching file paths with a regex, so there is some precedent.

> **···**
>
> > On Sep 4, 2007, at 12:00 PM, kazaam wrote:
> > 
> > > On Wed, 5 Sep 2007 00:52:57 +0900 \>\> Daniel Berger \<djberg96@gmail.com\> wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![Lionel\_Bouton](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lionel\_Bouton](https://rubytalk.org/u/Lionel_Bouton)\
**Post date:** [4 September 2007 22:08 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376/8 "2007-09-04T22:08:25Z")

</div>

Joel VanderWerf wrote the following on 04.09.2007 23:47 :

> Gary Wright wrote:
> 
> > > > Dir["fglrx\*"].each{ |file| File.delete(file) }
> > > 
> > > that's pretty much the same like: system('rm -f fglrx\*') but I'm  
> > > asking why one doesn't allow regexpr into File.exists?
> > 
> > I would guess it is because file name pattern matching  
> > is subtly different from string pattern matching. In  
> > particular you have the problem of directory hierarchies  
> > and how to handle the separator character (typically '/').  
> > The common file pattern syntax is also \*not\* the same  
> > as general regular expressions.
> 
> The slocate(1) program does have a -r option for searching file paths  
> with a regex, so there is some precedent.

slocate uses it's own database, not direct file access. File.exists?  
uses a system call directly. Using regexes would need to list all files  
that could match (a directory or the whole virtual filesystem depending  
on what you are matching with: a filename in a given directory or a  
path). This is far from a common need and in the general case far too  
slow: developpers are better off coding the regex matching code  
themselves and optimize it by listing the smallest subset of filepaths  
the OS can give them before they filter with a regex (which is trivial  
to do in Ruby anyway)...

Lionel

> **···**
>
> > > On Sep 4, 2007, at 12:00 PM, kazaam wrote:
> > > 
> > > > On Wed, 5 Sep 2007 00:52:57 +0900 \>\>\> Daniel Berger \<djberg96@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [4 September 2007 22:20 UTC](https://rubytalk.org/t/why-does-file-exists-not-support-regexp-it-would-be-very-handfull/40376/9 "2007-09-04T22:20:51Z")

</div>

Lionel Bouton wrote:

> Joel VanderWerf wrote the following on 04.09.2007 23:47 :
> 
> > Gary Wright wrote:
> > 
> > > > > Dir["fglrx\*"].each{ |file| File.delete(file) }
> > > > 
> > > > that's pretty much the same like: system('rm -f fglrx\*') but I'm  
> > > > asking why one doesn't allow regexpr into File.exists?
> > > 
> > > I would guess it is because file name pattern matching  
> > > is subtly different from string pattern matching. In  
> > > particular you have the problem of directory hierarchies  
> > > and how to handle the separator character (typically '/').  
> > > The common file pattern syntax is also \*not\* the same  
> > > as general regular expressions.
> > 
> > The slocate(1) program does have a -r option for searching file paths  
> > with a regex, so there is some precedent.
> 
> slocate uses it's own database, not direct file access. File.exists?  
> uses a system call directly. Using regexes would need to list all files  
> that could match (a directory or the whole virtual filesystem depending  
> on what you are matching with: a filename in a given directory or a  
> path). This is far from a common need and in the general case far too  
> slow: developpers are better off coding the regex matching code  
> themselves and optimize it by listing the smallest subset of filepaths  
> the OS can give them before they filter with a regex (which is trivial  
> to do in Ruby anyway)...

Well, there's "find -regex", too, but I agree that regex matching is probably better for narrowing down a globbed search.

> **···**
>
> > > > On Sep 4, 2007, at 12:00 PM, kazaam wrote:
> > > > 
> > > > > On Wed, 5 Sep 2007 00:52:57 +0900 \>\>\>\> Daniel Berger \<djberg96@gmail.com\> wrote:
> 
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
