"require" with many files

Hi, is there any preferred way to load/require all the fiels in a directory?
For example, my main file does:

  require "header.rb"

and file "header.rb" must load all the related files contained in
"headers/" directory. I do the following in "header.rb":

  headers_path = File.dirname(__FILE__) + '/headers'
  Dir.chdir(headers_path)
  Dir["*.rb"].each do |file|
      require "#{headers_path}/#{file}"
  end

Is it enought ok or can it receive some Ruby's magic? XD

Thanks for any suggestion.

···

--
Iñaki Baz Castillo
<ibc@aliax.net>

I've never seen anything else

···

On Tue, Apr 29, 2008 at 2:58 AM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

Hi, is there any preferred way to load/require all the fiels in a directory?
For example, my main file does:

  require "header.rb"

and file "header.rb" must load all the related files contained in
"headers/" directory. I do the following in "header.rb":

  headers_path = File.dirname(__FILE__) + '/headers'
  Dir.chdir(headers_path)
  Dir["*.rb"].each do |file|
      require "#{headers_path}/#{file}"
  end

Is it enought ok or can it receive some Ruby's magic? XD

Thanks for any suggestion.

--
Iñaki Baz Castillo
<ibc@aliax.net>

Iñaki Baz Castillo wrote:

Hi, is there any preferred way to load/require all the fiels
in a directory?
For example, my main file does:

  require "header.rb"

and file "header.rb" must load all the related files contained in
"headers/" directory. I do the following in "header.rb":

  headers_path = File.dirname(__FILE__) + '/headers'
  Dir.chdir(headers_path) Dir["*.rb"].each do |file|
      require "#{headers_path}/#{file}"
  end

Is it enought ok or can it receive some Ruby's magic? XD

Thanks for any suggestion.

You do use Ruby magic here ;-). The only thing I would suggest is that if you do not have to set the current directory to headers_path for some other reason, use this variation:

  Dir[File.join(File.dirname(__FILE__), 'headers', "*.rb")].each do |file|
      require file
  end

Gennady.

glob = File.join( File.dirname(__FILE__), 'headers', '*.rb' )

Dir.glob(glob){|lib| require lib}

is a bit shorter...

a @ http://codeforpeople.com/

···

On Apr 29, 2008, at 2:58 AM, Iñaki Baz Castillo wrote:

headers_path = File.dirname(__FILE__) + '/headers'
Dir.chdir(headers_path)
Dir["*.rb"].each do |file|
     require "#{headers_path}/#{file}"
end

Is it enought ok or can it receive some Ruby's magic? XD

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

If you say

$: << theDirectory

then you can just require the files by name, I believe... m.

···

Iñaki Baz Castillo <ibc@aliax.net> wrote:

Hi, is there any preferred way to load/require all the fiels in a directory?
For example, my main file does:

  require "header.rb"

and file "header.rb" must load all the related files contained in
"headers/" directory. I do the following in "header.rb":

  headers_path = File.dirname(__FILE__) + '/headers'
  Dir.chdir(headers_path)
  Dir["*.rb"].each do |file|
      require "#{headers_path}/#{file}"
  end

Is it enought ok or can it receive some Ruby's magic?

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
Leopard - http://www.takecontrolbooks.com/leopard-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

I seem to recall proposals for allowing something like "require header/
*" ala Java's import (maybe I brought it up myself?) but I can't
remember what the arguments against it were.

Dan

···

On Apr 29, 11:33 am, "ara.t.howard" <ara.t.how...@gmail.com> wrote:

On Apr 29, 2008, at 2:58 AM, Iñaki Baz Castillo wrote:

> headers_path = File.dirname(__FILE__) + '/headers'
> Dir.chdir(headers_path)
> Dir["*.rb"].each do |file|
> require "#{headers_path}/#{file}"
> end

> Is it enought ok or can it receive some Ruby's magic? XD

glob = File.join( File.dirname(__FILE__), 'headers', '*.rb' )

Dir.glob(glob){|lib| require lib}

is a bit shorter...

The problem is that I need to load before some specifi files because have
modules that others files require:

headers_path = File.dirname(__FILE__) + '/headers'
Dir.chdir(headers_path)

require "#{headers_path}/header.rb"
require "#{headers_path}/modules.rb"

Dir["hdr_*.rb"].each do |file|
  require "#{headers_path}/#{file}"
end

But ok, thanks a lot for all your suggestions :slight_smile:

···

El Martes, 29 de Abril de 2008, Gennady Bystritsky escribió:

You do use Ruby magic here ;-). The only thing I would suggest is that if
you do not have to set the current directory to headers_path for some other
reason, use this variation:

  Dir[File.join(File.dirname(__FILE__), 'headers', "*.rb")].each do |file|
      require file
  end

--
Iñaki Baz Castillo