[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

getting the user's home directory path

Chad Perrin

3/20/2008 7:05:00 AM

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.ap... ]
Kent Beck: "I always knew that one day Smalltalk would replace Java. I
just didn't know it would be called Ruby."

5 Answers

Alex Young

3/20/2008 8:27:00 AM

0


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


Chad Perrin

3/20/2008 4:51:00 PM

0

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.ap... ]
Kent Beck: "I always knew that one day Smalltalk would replace Java. I
just didn't know it would be called Ruby."

Alex Young

3/20/2008 5:16:00 PM

0

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


Nobuyoshi Nakada

3/20/2008 9:16:00 PM

0

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

Chad Perrin

3/21/2008 11:23:00 PM

0

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