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

Discussion in 'Ruby' started by Eddy Xu, Apr 11, 2008.

  1. Eddy Xu

    Eddy Xu Guest

    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?
    Eddy Xu, Apr 11, 2008
    #1
    1. Advertising

  2. Eddy Xu

    Saku Ytti Guest

    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
    Saku Ytti, Apr 11, 2008
    #2
    1. Advertising

  3. Eddy Xu

    Eddy Xu Guest

    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 <> 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
    Eddy Xu, Apr 11, 2008
    #3
  4. Eddy Xu

    Chris Hulan Guest

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

    Cheers
    Chris Hulan, Apr 11, 2008
    #4
  5. Eddy Xu

    Saku Ytti Guest

    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
    Saku Ytti, Apr 11, 2008
    #5
  6. Eddy Xu

    Eddy Xu Guest

    Thank you all.

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

    On Apr 12, 1:55 am, Saku Ytti <> 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
    Eddy Xu, Apr 11, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ESPN Lover

    Which way is best to read from a file?

    ESPN Lover, Oct 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    369
    ESPN Lover
    Oct 13, 2004
  2. Bill Henning

    Best way to require SSL for a page?

    Bill Henning, Jan 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    422
    Scott Allen
    Jan 3, 2005
  3. Familyman
    Replies:
    3
    Views:
    511
    Alan J. Flavell
    Feb 9, 2006
  4. Zev Blut
    Replies:
    10
    Views:
    175
    Zev Blut
    Feb 23, 2005
  5. Bernard Lambeau
    Replies:
    6
    Views:
    156
    Bernard Lambeau
    Jan 14, 2011
Loading...

Share This Page