Lazy requires


#1

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


#2

On Nov 17, 6:36 am, aidy 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?


#3

Define good.


#4

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


#5

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.