There must be a better way -- requiring multiple files

D

David Teare

Hi all,

I want to require all ruby files from a "lib" directory. This is
what I did so far:

lib_path = File.expand_path(__FILE__ + "/../../lib/")
Dir.new(lib_path).entries.each { |f| require lib_path + '/' + f
if f =~ /.*\.rb/ }

Is there an easier way?

Thanks!
--Dave.
 
R

Robert Klemme

David said:
Hi all,

I want to require all ruby files from a "lib" directory. This is
what I did so far:

lib_path = File.expand_path(__FILE__ + "/../../lib/")
Dir.new(lib_path).entries.each { |f| require lib_path + '/' + f
if f =~ /.*\.rb/ }

Is there an easier way?

Dir[File.join(__FILE__, "..", "..", "lib", "*.rb")].each {|l| require l}

recursive:

Dir[File.join(__FILE__, "..", "..", "lib", "**", "*.rb")].each {|l|
require l}

Kind regards

robert
 
S

Stefan Lang

]
Dir[File.join(__FILE__, "..", "..", "lib", "**", "*.rb")].each {|l|
require l}
[..]

A side note:

I have come to the conclusion that the following syntax makes
more sense for Dir[]/Dir.glob:

Dir["#{__FILE__}/../../lib/**/*.rb"]

(In other words, File.join doesn't make sense here.)

Why?

1) In my opinion, it is more readable
2) After reading dir.c in Ruby's sources, I would say
that it is at least as portable as the File.join variant.
Others more familiar with the Ruby source already said
that Ruby uses slashes internally on all platforms.
3) It's more efficient.


Kind regards,
Stefan
 
R

Robert Klemme

Stefan said:
]
Dir[File.join(__FILE__, "..", "..", "lib", "**", "*.rb")].each {|l|
require l}
[..]

A side note:

I have come to the conclusion that the following syntax makes
more sense for Dir[]/Dir.glob:

Dir["#{__FILE__}/../../lib/**/*.rb"]

(In other words, File.join doesn't make sense here.)

Why?

1) In my opinion, it is more readable

Yeah, probably.
2) After reading dir.c in Ruby's sources, I would say
that it is at least as portable as the File.join variant.
Others more familiar with the Ruby source already said
that Ruby uses slashes internally on all platforms.

I'm a bit wary to rely on such knowledge about internals. I prefer to
stick with the interfaces.

But then again it might be a bit too much formalism in this case - and
especially for one shot scripts.
3) It's more efficient.

Yep, guess so.

Interesting points raised nevertheless.

Kind regards

robert
 
W

William James

Stefan said:
]
Dir[File.join(__FILE__, "..", "..", "lib", "**", "*.rb")].each {|l|
require l}
[..]

A side note:

I have come to the conclusion that the following syntax makes
more sense for Dir[]/Dir.glob:

Dir["#{__FILE__}/../../lib/**/*.rb"]

(In other words, File.join doesn't make sense here.)

Why?

1) In my opinion, it is more readable
2) After reading dir.c in Ruby's sources, I would say
that it is at least as portable as the File.join variant.
Others more familiar with the Ruby source already said
that Ruby uses slashes internally on all platforms.

I think that all unix-influenced programs that I use under
windoze accept either slashes or backslashes.
These definitely do:
unzip, zip, awk, gawk, mawk, less, grep, sed, cat, vile, tail, diff,
strings
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top