Relative Path in ruby - for File open

Discussion in 'Ruby' started by Shekar Ls, Aug 19, 2009.

  1. Shekar Ls

    Shekar Ls Guest

    Hi Guys,
    I am trying to fetch a file to do some processing in ruby,
    instead of giving the absolute path i wanted to give relative path.

    File.open("config.txt") do |source|
    source.each_line do |line|
    if line =~ /^(\w+)\s*=\s*"(.*)"\s*$/
    ....

    In the above mentioned example, i want config.txt to be accessed from
    the current path whichever user runs it.

    Cheers
    --
    Posted via http://www.ruby-forum.com/.
    Shekar Ls, Aug 19, 2009
    #1
    1. Advertising

  2. Shekar Ls

    Ryan Davis Guest

    On Aug 18, 2009, at 22:02 , Shekar Ls wrote:

    > Hi Guys,
    > I am trying to fetch a file to do some processing in ruby,
    > instead of giving the absolute path i wanted to give relative path.
    >
    > File.open("config.txt") do |source|
    > source.each_line do |line|
    > if line =~ /^(\w+)\s*=\s*"(.*)"\s*$/
    > ....
    >
    > In the above mentioned example, i want config.txt to be accessed from
    > the current path whichever user runs it.


    what's the problem?
    Ryan Davis, Aug 19, 2009
    #2
    1. Advertising

  3. Shekar Ls

    Ben Giddings Guest

    On Aug 19, 2009, at 01:02, Shekar Ls wrote:
    > In the above mentioned example, i want config.txt to be accessed from
    > the current path whichever user runs it.


    By "current path" do you mean executable path or the current working
    directory of the script?

    If you mean the current working directory (i.e. ENV['PWD']) everything
    should work as you expect. Just do File.open("config.txt")

    Ben
    Ben Giddings, Aug 19, 2009
    #3
  4. Ben Giddings wrote:
    > On Aug 19, 2009, at 01:02, Shekar Ls wrote:
    >> In the above mentioned example, i want config.txt to be accessed from
    >> the current path whichever user runs it.

    >
    > By "current path" do you mean executable path or the current working
    > directory of the script?
    >
    > If you mean the current working directory (i.e. ENV['PWD']) everything
    > should work as you expect. Just do File.open("config.txt")


    And if you mean the directory in which the script is located, then

    AppRoot = File.expand_path(File.dirname(__FILE__))

    File.open(File.join(AppRoot, "config.txt")) do |source|
    ..
    end
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Aug 19, 2009
    #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. Matt
    Replies:
    3
    Views:
    19,511
    Sudsy
    Jul 8, 2004
  2. Thomas Guettler
    Replies:
    3
    Views:
    741
    Andrei
    Oct 27, 2003
  3. Oren
    Replies:
    1
    Views:
    526
    Michael D. Ober
    Apr 29, 2007
  4. Nigel Wilkinson
    Replies:
    2
    Views:
    376
    Nigel Wilkinson
    Jul 25, 2005
  5. skieros
    Replies:
    14
    Views:
    195
    Michele Dondi
    May 2, 2007
Loading...

Share This Page