# Lazy requires

**URL:** <https://www.ruby-forum.com/t/lazy-requires/153451>\
**Category:** Ruby\
**Created:** [November 17, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/lazy-requires/153451 "2008-11-17T12:40:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [November 17, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/lazy-requires/153451/1 "2008-11-17T12:40:44Z")

</div>

Hi,

Are lazy requires generally a good idea

def require\_files(path)  
dir = File.dirname( **FILE** )  
Dir[File.expand\_path("#{dir}/#{path}")].uniq.each do |file|  
require file  
end  
end

?

Thanks

Aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [November 17, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/lazy-requires/153451/2 "2008-11-17T14:36:05Z")

</div>

On Nov 17, 6:36 am, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> Are lazy requires generally a good idea
> 
> def require\_files(path)  
> dir = File.dirname( **FILE** )  
> Dir[File.expand\_path(“#{dir}/#{path}”)].uniq.each do |file|  
> require file  
> end  
> end

Why do you call that lazy? Do you mean “dynamic” requires?

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [November 18, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/lazy-requires/153451/3 "2008-11-18T14:15:45Z")

</div>

Define good.

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [November 18, 2008, 2:42pm UTC](https://www.ruby-forum.com/t/lazy-requires/153451/4 "2008-11-18T14:42:50Z")

</div>

Hi,

On Nov 17, 2008, at 5:36 AM, aidy wrote:

> Are lazy requires generally a good idea
> 
> def require\_files(path)  
> dir = File.dirname( **FILE** )  
> Dir[File.expand\_path("#{dir}/#{path}")].uniq.each do |file|  
> require file  
> end  
> end
> 
> ?

I also am not sure what you mean by “lazy” or “good”, but offer this:

Requiring a full path can become messy with double requires if you  
require the same file anywhere else in your project. It is sometimes  
preferred to add the dir to your load paths and require the file’s  
base name, instead.

Other things to consider

- Array#uniq isn’t needed here; a globbed path will return all files  
only once.
- “map” might be a better choice than “each”, as you can determine  
from output the state of all required libs.

Stephen

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [November 19, 2008, 11:39am UTC](https://www.ruby-forum.com/t/lazy-requires/153451/5 "2008-11-19T11:39:20Z")

</div>

aidy wrote:

> Hi,
> 
> Are lazy requires generally a good idea
> 
> def require\_files(path)  
> dir = File.dirname( **FILE** )  
> Dir[File.expand\_path("#{dir}/#{path}")].uniq.each do |file|  
> require file  
> end  
> end

It’s certainly accepted practice, but with all things you need to ask if  
it’s necessary.

If you know the files in the path you want, it’s much more reliable to  
specify them.
