lazy requires

A

aidy

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
 
T

Trans

Hi,

Are lazy requires generally a good idea

def require_files(path)
=A0 dir =3D File.dirname(__FILE__)
=A0 Dir[File.expand_path("#{dir}/#{path}")].uniq.each do |file|
=A0 =A0 require file
=A0 end
end

Why do you call that lazy? Do you mean "dynamic" requires?
 
R

Ron Fox

Define good.
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
 
S

Stephen Celis

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

?

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
 
E

Erik Hollensbe

aidy said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top