getting the user's home directory path

Discussion in 'Ruby' started by Chad Perrin, Mar 20, 2008.

  1. Chad Perrin

    Chad Perrin Guest

    I've got a program that needs to access a file in the ~/etc/ directory.
    This program currently accesses it like so:

    file_path = File.expand_path("~#{ENV['USER']}/etc/#{filename}")

    . . but that environment variable just looks hideous in there, at least
    to me. In Perl, I'm used to using getpwuid() instead of $_ENV['user'].
    Is there some equivalent to that in Ruby, or am I stuck with ENV['USER']?

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    Kent Beck: "I always knew that one day Smalltalk would replace Java. I
    just didn't know it would be called Ruby."
    Chad Perrin, Mar 20, 2008
    #1
    1. Advertising

  2. Chad Perrin

    Alex Young Guest

    On Thu, 2008-03-20 at 16:05 +0900, Chad Perrin wrote:
    > I've got a program that needs to access a file in the ~/etc/ directory.
    > This program currently accesses it like so:
    >
    > file_path = File.expand_path("~#{ENV['USER']}/etc/#{filename}")
    >
    > . . . but that environment variable just looks hideous in there, at least
    > to me. In Perl, I'm used to using getpwuid() instead of $_ENV['user'].
    > Is there some equivalent to that in Ruby, or am I stuck with ENV['USER']?
    >


    require 'etc'
    Etc.getpwuid.dir

    works for me...

    --
    Alex
    Alex Young, Mar 20, 2008
    #2
    1. Advertising

  3. Chad Perrin

    Chad Perrin Guest

    On Thu, Mar 20, 2008 at 05:27:03PM +0900, Alex Young wrote:
    >
    > On Thu, 2008-03-20 at 16:05 +0900, Chad Perrin wrote:
    > > I've got a program that needs to access a file in the ~/etc/ directory.
    > > This program currently accesses it like so:
    > >
    > > file_path = File.expand_path("~#{ENV['USER']}/etc/#{filename}")
    > >
    > > . . . but that environment variable just looks hideous in there, at least
    > > to me. In Perl, I'm used to using getpwuid() instead of $_ENV['user'].
    > > Is there some equivalent to that in Ruby, or am I stuck with ENV['USER']?
    > >

    >
    > require 'etc'
    > Etc.getpwuid.dir
    >
    > works for me...


    Hallelujah. That's exactly what I wanted. Thank you much.

    I don't know why this wasn't working:

    > ri getpwuid

    Nothing known about getpwuid

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    Kent Beck: "I always knew that one day Smalltalk would replace Java. I
    just didn't know it would be called Ruby."
    Chad Perrin, Mar 20, 2008
    #3
  4. Chad Perrin

    Alex Young Guest

    On Fri, 2008-03-21 at 01:50 +0900, Chad Perrin wrote:
    > On Thu, Mar 20, 2008 at 05:27:03PM +0900, Alex Young wrote:
    > >
    > > On Thu, 2008-03-20 at 16:05 +0900, Chad Perrin wrote:
    > > > I've got a program that needs to access a file in the ~/etc/ directory.
    > > > This program currently accesses it like so:
    > > >
    > > > file_path = File.expand_path("~#{ENV['USER']}/etc/#{filename}")
    > > >
    > > > . . . but that environment variable just looks hideous in there, at least
    > > > to me. In Perl, I'm used to using getpwuid() instead of $_ENV['user'].
    > > > Is there some equivalent to that in Ruby, or am I stuck with ENV['USER']?
    > > >

    > >
    > > require 'etc'
    > > Etc.getpwuid.dir
    > >
    > > works for me...

    >
    > Hallelujah. That's exactly what I wanted. Thank you much.
    >
    > I don't know why this wasn't working:
    >
    > > ri getpwuid

    > Nothing known about getpwuid


    I get:
    > qri getpwuid

    nil

    Same problem. Documentation seems to be rather hit-and-miss all round
    these days, but I haven't got any time to contribute to help. I was
    lucky with this - I managed to trawl it out of the depths of my memory
    from a few months ago, when I needed something else in Etc.

    --
    Alex
    Alex Young, Mar 20, 2008
    #4
  5. Hi,

    At Thu, 20 Mar 2008 16:05:12 +0900,
    Chad Perrin wrote in [ruby-talk:295141]:
    > I've got a program that needs to access a file in the ~/etc/ directory.
    > This program currently accesses it like so:
    >
    > file_path = File.expand_path("~#{ENV['USER']}/etc/#{filename}")


    File.expand_path("~/etc/#{filename}")

    --
    Nobu Nakada
    Nobuyoshi Nakada, Mar 20, 2008
    #5
  6. Chad Perrin

    Chad Perrin Guest

    On Fri, Mar 21, 2008 at 06:16:02AM +0900, Nobuyoshi Nakada wrote:
    > Hi,
    >
    > At Thu, 20 Mar 2008 16:05:12 +0900,
    > Chad Perrin wrote in [ruby-talk:295141]:
    > > I've got a program that needs to access a file in the ~/etc/ directory.
    > > This program currently accesses it like so:
    > >
    > > file_path = File.expand_path("~#{ENV['USER']}/etc/#{filename}")

    >
    > File.expand_path("~/etc/#{filename}")


    Amazing -- and confusing. I could swear I actually tried that and it
    didn't work out for me. I guess I must have dreamed that.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    Phillip J. Haack: "Productivity is not about speed. It's about velocity.
    You can be fast, but if you're going in the wrong direction, you're not
    helping anyone."
    Chad Perrin, Mar 21, 2008
    #6
  7. Chad Perrin

    Guest

    It's 4 years late I know, but I found this:

    http://stackoverflow.com/a/4194280/20654

    File.expand_path('~')

    So the next guy that reach this via google get the right answer ;)

    On Friday, March 21, 2008 5:22:30 PM UTC-6, Chad Perrin wrote:
    > On Fri, Mar 21, 2008 at 06:16:02AM +0900, Nobuyoshi Nakada wrote:
    > > Hi,
    > >
    > > At Thu, 20 Mar 2008 16:05:12 +0900,
    > > Chad Perrin wrote in [ruby-talk:295141]:
    > > > I've got a program that needs to access a file in the ~/etc/ directory.
    > > > This program currently accesses it like so:
    > > >
    > > > file_path = File.expand_path("~#{ENV['USER']}/etc/#{filename}")

    > >
    > > File.expand_path("~/etc/#{filename}")

    >
    > Amazing -- and confusing. I could swear I actually tried that and it
    > didn't work out for me. I guess I must have dreamed that.
    >
    > --
    > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    > Phillip J. Haack: "Productivity is not about speed. It's about velocity.
    > You can be fast, but if you're going in the wrong direction, you're not
    > helping anyone."
    , Aug 11, 2012
    #7
  8. Chad Perrin

    OscarRyz Guest

    On Friday, March 21, 2008 5:22:30 PM UTC-6, Chad Perrin wrote:
    > On Fri, Mar 21, 2008 at 06:16:02AM +0900, Nobuyoshi Nakada wrote:
    > > Hi,
    > >
    > > At Thu, 20 Mar 2008 16:05:12 +0900,
    > > Chad Perrin wrote in [ruby-talk:295141]:
    > > > I've got a program that needs to access a file in the ~/etc/ directory.
    > > > This program currently accesses it like so:
    > > >
    > > > file_path = File.expand_path("~#{ENV['USER']}/etc/#{filename}")

    > >
    > > File.expand_path("~/etc/#{filename}")

    >
    > Amazing -- and confusing. I could swear I actually tried that and it
    > didn't work out for me. I guess I must have dreamed that.
    >
    > --
    > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    > Phillip J. Haack: "Productivity is not about speed. It's about velocity.
    > You can be fast, but if you're going in the wrong direction, you're not
    > helping anyone."




    On Friday, March 21, 2008 5:22:30 PM UTC-6, Chad Perrin wrote:
    > On Fri, Mar 21, 2008 at 06:16:02AM +0900, Nobuyoshi Nakada wrote:
    > > Hi,
    > >
    > > At Thu, 20 Mar 2008 16:05:12 +0900,
    > > Chad Perrin wrote in [ruby-talk:295141]:
    > > > I've got a program that needs to access a file in the ~/etc/ directory.
    > > > This program currently accesses it like so:
    > > >
    > > > file_path = File.expand_path("~#{ENV['USER']}/etc/#{filename}")

    > >
    > > File.expand_path("~/etc/#{filename}")

    >
    > Amazing -- and confusing. I could swear I actually tried that and it
    > didn't work out for me. I guess I must have dreamed that.
    >
    > --
    > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    > Phillip J. Haack: "Productivity is not about speed. It's about velocity.
    > You can be fast, but if you're going in the wrong direction, you're not
    > helping anyone."
    OscarRyz, Aug 11, 2012
    #8
    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. john
    Replies:
    4
    Views:
    654
    Jim Higson
    Jun 15, 2006
  2. K.S.Sreeram
    Replies:
    0
    Views:
    325
    K.S.Sreeram
    Jul 18, 2006
  3. Edward Diener
    Replies:
    5
    Views:
    1,113
    Edward Diener
    Jul 8, 2007
  4. nancyflorida
    Replies:
    0
    Views:
    320
    nancyflorida
    Nov 12, 2007
  5. Martin Ankerl

    Path of Home directory

    Martin Ankerl, Aug 18, 2004, in forum: Ruby
    Replies:
    6
    Views:
    111
    Martin Ankerl
    Aug 18, 2004
Loading...

Share This Page