getting the user's home directory path

C

Chad Perrin

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']?
 
A

Alex Young

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...
 
C

Chad Perrin

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
 
A

Alex Young

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.
 
N

Nobuyoshi Nakada

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}")
 
C

Chad Perrin

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.
 
O

oscarryz

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

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."
 
O

OscarRyz

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."



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."
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top