# There must be a better way -- requiring multiple files

**URL:** <https://www.ruby-forum.com/t/there-must-be-a-better-way-requiring-multiple-files/48599>\
**Category:** Ruby\
**Created:** [November 22, 2005, 4:34pm UTC](https://www.ruby-forum.com/t/there-must-be-a-better-way-requiring-multiple-files/48599 "2005-11-22T16:34:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dteare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@dteare](https://www.ruby-forum.com/u/dteare)\
**Post date:** [November 22, 2005, 4:34pm UTC](https://www.ruby-forum.com/t/there-must-be-a-better-way-requiring-multiple-files/48599/1 "2005-11-22T16:34:05Z")

</div>

Hi all,

I want to require all ruby files from a “lib” directory. This is  
what I did so far:

```
 lib_path = File.expand_path( __FILE__ + "/../../lib/")
 Dir.new(lib_path).entries.each { |f| require lib_path + '/' + f

```

if f =~ /.\*.rb/ }

Is there an easier way?

Thanks!  
–Dave.

---

<div class="post-metadata">

**Author:** ![dteare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@dteare](https://www.ruby-forum.com/u/dteare)\
**Post date:** [November 22, 2005, 4:38pm UTC](https://www.ruby-forum.com/t/there-must-be-a-better-way-requiring-multiple-files/48599/2 "2005-11-22T16:38:06Z")

</div>

David Teare wrote:

> Hi all,
> 
> I want to require all ruby files from a “lib” directory. This is  
> what I did so far:
> 
> ```
> lib_path = File.expand_path( __FILE__ + "/../../lib/")
> Dir.new(lib_path).entries.each { |f| require lib_path + '/' + f
> 
> ```
> 
> if f =~ /.\*.rb/ }
> 
> Is there an easier way?

Dir[File.join( **FILE** , “…”, “…”, “lib”, “\*.rb”)].each {|l| require l}

recursive:

Dir[File.join( **FILE** , “…”, “…”, “lib”, “\*\*”, “\*.rb”)].each {|l|  
require l}

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![dteare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@dteare](https://www.ruby-forum.com/u/dteare)\
**Post date:** [November 22, 2005, 5:10pm UTC](https://www.ruby-forum.com/t/there-must-be-a-better-way-requiring-multiple-files/48599/3 "2005-11-22T17:10:19Z")

</div>

On Tuesday 22 November 2005 16:37, Robert K. wrote:  
[…]

> Dir[File.join( **FILE** , “…”, “…”, “lib”, “\*\*”, “\*.rb”)].each {|l|  
> require l}  
> […]

A side note:

I have come to the conclusion that the following syntax makes  
more sense for Dir[]/Dir.glob:

Dir["#{ **FILE** }/…/…/lib/\*\*/\*.rb"]

(In other words, File.join doesn’t make sense here.)

Why?

1. In my opinion, it is more readable
2. After reading dir.c in Ruby’s sources, I would say  
that it is at least as portable as the File.join variant.  
Others more familiar with the Ruby source already said  
that Ruby uses slashes internally on all platforms.
3. It’s more efficient.

Kind regards,  
Stefan

---

<div class="post-metadata">

**Author:** ![dteare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@dteare](https://www.ruby-forum.com/u/dteare)\
**Post date:** [November 22, 2005, 8:59pm UTC](https://www.ruby-forum.com/t/there-must-be-a-better-way-requiring-multiple-files/48599/4 "2005-11-22T20:59:56Z")

</div>

Stefan L. wrote:

> that Ruby uses slashes internally on all platforms.  
> I think that all unix-influenced programs that I use under  
> windoze accept either slashes or backslashes.  
> These definitely do:  
> unzip, zip, awk, gawk, mawk, less, grep, sed, cat, vile, tail, diff,  
> strings

---

<div class="post-metadata">

**Author:** ![dteare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8e7dd6/32.png) [@dteare](https://www.ruby-forum.com/u/dteare)\
**Post date:** [November 22, 2005, 5:58pm UTC](https://www.ruby-forum.com/t/there-must-be-a-better-way-requiring-multiple-files/48599/5 "2005-11-22T17:58:42Z")

</div>

Stefan L. wrote:

> Dir["#{ **FILE** }/…/…/lib/\*\*/\*.rb"]
> 
> (In other words, File.join doesn’t make sense here.)
> 
> Why?
> 
> 1. In my opinion, it is more readable

Yeah, probably.

> 1. After reading dir.c in Ruby’s sources, I would say  
> that it is at least as portable as the File.join variant.  
> Others more familiar with the Ruby source already said  
> that Ruby uses slashes internally on all platforms.

I’m a bit wary to rely on such knowledge about internals. I prefer to  
stick with the interfaces.

But then again it might be a bit too much formalism in this case - and  
especially for one shot scripts.

> 1. It’s more efficient.

Yep, guess so.

Interesting points raised nevertheless.

Kind regards

```
robert
```
