Lazy requires

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

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

Define good.

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs