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']?
     
    Chad Perrin, Mar 20, 2008
    #1
    1. Advertisements

  2. Chad Perrin

    Alex Young Guest

    require 'etc'
    Etc.getpwuid.dir

    works for me...
     
    Alex Young, Mar 20, 2008
    #2
    1. Advertisements

  3. Chad Perrin

    Chad Perrin Guest

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

    I don't know why this wasn't working:
    Nothing known about getpwuid
     
    Chad Perrin, Mar 20, 2008
    #3
  4. Chad Perrin

    Alex Young Guest

    I get:
    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 Young, Mar 20, 2008
    #4
  5. Hi,

    At Thu, 20 Mar 2008 16:05:12 +0900,
    Chad Perrin wrote in [ruby-talk:295141]:
    File.expand_path("~/etc/#{filename}")
     
    Nobuyoshi Nakada, Mar 20, 2008
    #5
  6. Chad Perrin

    Chad Perrin Guest

    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.
     
    Chad Perrin, Mar 21, 2008
    #6
  7. Chad Perrin

    oscarryz 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 ;)

     
    oscarryz, Aug 11, 2012
    #7
  8. Chad Perrin

    OscarRyz Guest



     
    OscarRyz, Aug 11, 2012
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.