Which way is the best way to require another file in .rb

E

Eddy Xu

I put all source in to lib/ path, and named a lib/foo path for my foo
project. However, is there a better approach to require the ruby
source which in lib/foo/ from the file in the same path.

For example, lib/foo/db.rb require lib/foo/configure.rb, currently I
write:
require "#{File.dirname(__FILE__)}/../configure"

But it is a ugly way to require other files. Would anyone explain a
more "standard" approach to do it?
 
S

Saku Ytti

require "#{File.dirname(__FILE__)}/../configure"

But it is a ugly way to require other files. Would anyone explain a
more "standard" approach to do it?

Push path to $:?
 
E

Eddy Xu

push it in every file or in the root file such as bin/foo.rb? if in
that case, it is impossible to run single source file property,
right?
 
C

Chris Hulan

I put all source in to lib/ path, and named a lib/foo path for my foo
project. However, is there a better approach to require the ruby
source which in lib/foo/ from the file in the same path.

For example, lib/foo/db.rb require lib/foo/configure.rb, currently I
write:
require "#{File.dirname(__FILE__)}/../configure"

But it is a ugly way to require other files. Would anyone explain a
more "standard" approach to do it?

The default load path includes '.', so if the file is in the same
directory it should be found.
HAve a look at "Where Ruby Finds Its Modules" section of
http://www.ruby-doc.org/docs/ProgrammingRuby/html/rubyworld.html
Despite its age I don;t think the current version of ruby differes

Cheers
 
S

Saku Ytti

push it in every file or in the root file such as bin/foo.rb? if in
that case, it is impossible to run single source file property,
right?

Sorry, I misunderstood it as ad'hoc requirement. If it's always
like that in your system, perhaps set environment
variable RUBYLIB? Or even recompile ruby?
 

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,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top