[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Eddy Xu

4/11/2008 5:33:00 PM

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?
5 Answers

Saku Ytti

4/11/2008 5:40:00 PM

0

On (2008-04-12 02:35 +0900), Eddy Xu wrote:

> 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 $:?

--
++ytti

Eddy Xu

4/11/2008 5:45:00 PM

0

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?

On Apr 12, 1:40 am, Saku Ytti <s...@ytti.fi> wrote:
> On (2008-04-12 02:35 +0900), Eddy Xu wrote:
>
> > 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 $:?
>
> --
>   ++ytti

Chris Hulan

4/11/2008 5:53:00 PM

0

On Apr 11, 1:32 pm, Eddy Xu <edd...@gmail.com> wrote:
> 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/ruby...
Despite its age I don;t think the current version of ruby differes

Cheers

Saku Ytti

4/11/2008 5:55:00 PM

0

On (2008-04-12 02:50 +0900), Eddy Xu wrote:

> 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?

--
++ytti

Eddy Xu

4/11/2008 6:26:00 PM

0

Thank you all.

I write require "foo/bar" and it works fine.

On Apr 12, 1:55 am, Saku Ytti <s...@ytti.fi> wrote:
> On (2008-04-12 02:50 +0900), Eddy Xu wrote:
>
> > 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?
>
> --
>   ++ytti