Getting a file location and name...

Discussion in 'Ruby' started by Joe Van Dyk, Oct 21, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    Hi,

    Say I have /joe/configuration/config.rb:
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D
    puts "this configuration file is located in this directory: " +
    File.dirname( ????? )
    eval File.read(File.dirname( ?????? ) + "/common.rb")
    # other stuff
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D

    common.rb is located in /joe/configuration. It also contains some Ruby cod=
    e.


    The main program looks something like this:
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D
    config_file =3D ARGV.shift
    eval File.read config_file
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D

    And I'd start the program via 'ruby main.rb /joe/configuration.rb'.

    So, I have a main program that reads in a Ruby configuration file that
    can also read other configuration files that are in a relative
    directory. I'm having problems with the relative directory part. If
    I hardcode the directory paths, everything's ok. But I don't want
    /joe/configuration/config.rb to know that it's in /joe/configuration.=20
    I just want that configuration file to be able to read
    /joe/configuration/common.rb.

    Sorry if this is confusing, let me know if I can clarify the question.

    Joe
     
    Joe Van Dyk, Oct 21, 2005
    #1
    1. Advertising

  2. On 10/21/05, Joe Van Dyk <> wrote:
    >
    > So, I have a main program that reads in a Ruby configuration file that
    > can also read other configuration files that are in a relative
    > directory. I'm having problems with the relative directory part. If
    > I hardcode the directory paths, everything's ok. But I don't want
    > /joe/configuration/config.rb to know that it's in /joe/configuration.
    > I just want that configuration file to be able to read
    > /joe/configuration/common.rb.
    >
    > Sorry if this is confusing, let me know if I can clarify the question.


    Don't worry Joe, this is a common problem, and fairly easily solved.
    All you have to do is use Ruby's built-in library search path code,
    with a special addition:

    config.rb:

    # Adds the directory containing this code to the search path
    $:.unshift File.dirname(File.expand_path(__FILE__))
    # Now require will work
    require 'common'

    Ryan
     
    Ryan Leavengood, Oct 21, 2005
    #2
    1. Advertising

  3. Joe Van Dyk wrote:
    > Hi,
    >
    > Say I have /joe/configuration/config.rb:
    > ==============================
    > puts "this configuration file is located in this directory: " +
    > File.dirname( ????? )
    > eval File.read(File.dirname( ?????? ) + "/common.rb")
    > # other stuff
    > ==============================
    >
    > common.rb is located in /joe/configuration. It also contains some Ruby code.
    >
    >
    > The main program looks something like this:
    > ==============================
    > config_file = ARGV.shift
    > eval File.read config_file
    > ==============================
    >
    > And I'd start the program via 'ruby main.rb /joe/configuration.rb'.
    >
    > So, I have a main program that reads in a Ruby configuration file that
    > can also read other configuration files that are in a relative
    > directory. I'm having problems with the relative directory part. If
    > I hardcode the directory paths, everything's ok. But I don't want
    > /joe/configuration/config.rb to know that it's in /joe/configuration.
    > I just want that configuration file to be able to read
    > /joe/configuration/common.rb.
    >
    > Sorry if this is confusing, let me know if I can clarify the question.
    >
    > Joe
    >


    What about something like

    def with_load_path(path)
    $LOAD_PATH.unshift path
    yield
    ensure
    $LOAD_PATH.shift
    end

    Then use that around your #eval call in the main program, with the
    '/joe/configuration' as the argument. Then require should work relative
    to the path you gave. (But note that $LOADED_FEATURES will prevent files
    with the same name from being loaded again, even if in a different
    config dir.)

    Not thread safe, but oh well.

    Another option: my "script" lib (see RAA). That makes sure that the
    global $LOADED_FEATURES doesn't get affected.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Oct 21, 2005
    #3
  4. Joe Van Dyk <> wrote:
    > Hi,
    >
    > Say I have /joe/configuration/config.rb:
    > ==============================
    > puts "this configuration file is located in this directory: " +
    > File.dirname( ????? )
    > eval File.read(File.dirname( ?????? ) + "/common.rb")
    > # other stuff
    > ==============================
    >
    > common.rb is located in /joe/configuration. It also contains some
    > Ruby code.
    >
    >
    > The main program looks something like this:
    > ==============================
    > config_file = ARGV.shift
    > eval File.read config_file


    IMHO eval is completely superfluous here: just do

    load config_file


    > ==============================
    >
    > And I'd start the program via 'ruby main.rb /joe/configuration.rb'.
    >
    > So, I have a main program that reads in a Ruby configuration file that
    > can also read other configuration files that are in a relative
    > directory. I'm having problems with the relative directory part. If
    > I hardcode the directory paths, everything's ok. But I don't want
    > /joe/configuration/config.rb to know that it's in /joe/configuration.
    > I just want that configuration file to be able to read
    > /joe/configuration/common.rb.


    If those other files do only belong to your config.rb (and are no files that
    you wish to require) then you can do this in config.rb

    base = File.basename __FILE__

    load File.join(base, "common.rb")

    File.open(File.join(base, "common.rb") do |io|
    ....
    end

    File.open(File.join(base, "etc", "lib", "whatever.conf") do |io|
    ....
    end

    Kind regards

    robert
     
    Robert Klemme, Oct 22, 2005
    #4
    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. Luke Dalessandro
    Replies:
    0
    Views:
    632
    Luke Dalessandro
    Jan 15, 2006
  2. saiho.yuen
    Replies:
    3
    Views:
    547
    kaeli
    Sep 14, 2004
  3. Replies:
    0
    Views:
    118
  4. Replies:
    2
    Views:
    116
  5. hehehe

    adf java and <location></location>

    hehehe, Jul 6, 2009, in forum: Javascript
    Replies:
    1
    Views:
    119
    Evertjan.
    Jul 6, 2009
Loading...

Share This Page