Forum: Ruby lazy requires

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee6788c2faaad7a5cd0bc466c9ea60d8?d=identicon&s=25 aidy (Guest)
on 2008-11-17 12:40
(Received via mailing list)
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
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2008-11-17 14:36
(Received via mailing list)
On Nov 17, 6:36 am, aidy <aidy.le...@googlemail.com> 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?
F6a834b9e424a1e5b199db450462b6cd?d=identicon&s=25 Ron Fox (Guest)
on 2008-11-18 14:15
(Received via mailing list)
Define good.
D68c97e8e2f1653b54c24493caf236ae?d=identicon&s=25 Stephen Celis (Guest)
on 2008-11-18 14:42
(Received via mailing list)
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
95ece3bc20c5dc43685302703a1e99bd?d=identicon&s=25 Erik Hollensbe (erikh)
on 2008-11-19 11:39
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 topic is locked and can not be replied to.