# Cross-platform Home Directory?

**URL:** <https://www.ruby-forum.com/t/cross-platform-home-directory/114647>\
**Category:** Ruby\
**Created:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647 "2007-09-17T13:23:35Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/1 "2007-09-17T13:23:35Z")

</div>

I have a little app that needs to store session data. I assume the  
best place to store it is in one’s home directory, but I need this app  
to be cross-platform. I glanced through all rbconfig.rb’s  
Config::CONFIG settings but did not see anything for it. How does one  
access a cross-platform home directory?

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/2 "2007-09-17T13:24:04Z")

</div>

There’s much more to this then I ever imagined. Thanks for all the  
answers, certainly enough to here to go on. Thanks.

T.

P.S. Seems like Launchy could use this functionality --maybe merge the  
Platform gem to make a strong single lib rather than two separate libs  
– just a thought.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/3 "2007-09-17T13:24:19Z")

</div>

Olivier R. wrote:

> This is how I do this :  
> home\_directory = Dir.chdir {|path| path}
> 
> The doc for Dir.chdir says that without argument, this method looks for  
> environnement variables HOME or LOGDIR. And with a block, the directory  
> is changed only inside the block, and restored right after.
> 
> –  
> Olivier R.

At first I would just check ENV to see if HOME or USERPROFILE was set.  
Then  
some one told me about the Platform gem 🙂

You know, the OOP thing gives me a nice idea of how to use that hehe.

TerryP.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/4 "2007-09-17T13:24:33Z")

</div>

Hi,

At Fri, 14 Sep 2007 04:19:04 +0900,  
Trans wrote in [ruby-talk:268970]:

> I have a little app that needs to store session data. I assume the  
> best place to store it is in one’s home directory, but I need this app  
> to be cross-platform. I glanced through all rbconfig.rb’s  
> Config::CONFIG settings but did not see anything for it. How does one  
> access a cross-platform home directory?

There is no static value.

FYI, in 1.9, if ENV[“HOME”] isn’t set, it will be set to

1. ENV[“HOMEDRIVE”] + ENV[“HOMEPATH”]
2. ENV[“USERPROFILE”] or
3. “Personal” special folder  
in the above order.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/5 "2007-09-17T13:24:50Z")

</div>

On 9/13/07, Trans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

What do you mean by “cross-platform home directory”? How is this  
directory  
accessed by the user, Samba, NTFS, some other means?

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 17, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/6 "2007-09-17T13:23:42Z")

</div>

Trans a écrit :

> I have a little app that needs to store session data. I assume the  
> best place to store it is in one’s home directory, but I need this app  
> to be cross-platform. I glanced through all rbconfig.rb’s  
> Config::CONFIG settings but did not see anything for it. How does one  
> access a cross-platform home directory?
> 
> Thanks,  
> T.

This is how I do this :  
home\_directory = Dir.chdir {|path| path}

The doc for Dir.chdir says that without argument, this method looks for  
environnement variables HOME or LOGDIR. And with a block, the directory  
is changed only inside the block, and restored right after.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/7 "2007-09-17T13:24:59Z")

</div>

Trans wrote:

> I have a little app that needs to store session data. I assume the  
> best place to store it is in one’s home directory

Everyone’s helped you with ways to find the home directory, but on  
Windows  
this is not normally the right place to store session data. That’s what  
Application Data is for. Make a directory under there for your  
application.  
Configuration data (user-modifiable but less frequently-changing) may be  
stored in the registry.

The Windows certification documents describe where you should put stuff.

Clifford H…

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/8 "2007-09-25T23:00:49Z")

</div>

Joel VanderWerf wrote:

> when /win32/

I think Mac OS X should have the traditional unix variables set, like  
${HOME}  
and ${EDITOR} but I don’t own a mac to find out :’(.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/9 "2007-09-25T23:02:03Z")

</div>

On 9/13/07, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > T.  
> > -Greg Graffin (Bad Religion)  
> > I probably meant NFS rather than NTFS

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/10 "2007-09-25T23:03:08Z")

</div>

> case RUBY\_PLATFORM  
> when /win32/  
> ENV[‘APPDATA’] ||  
> ENV[‘USERPROFILE’] ||  
> ENV[‘HOME’]

As HOME usually isn’t set on windows, the existence of the variable  
could indicate that the user would like to override the windows  
default. I would thus check for HOME first.

Also, under cygwin ruby all three variables are set, but APPDATA and  
USERPROFILE are “inherited” from the windows environment and are by  
default set as Windows path while HOME usually is set as cygwin path.  
In certain situations, this could cause certain difficulties, eg when  
calling external programs.

It seems the trick with chdir doesn’t work when neither HOME nor  
LOGDIR is set – as it is usually the case under Windows, I think.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/11 "2007-09-25T23:00:57Z")

</div>

On 9/13/07, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Thanks,  
> > > T.

Rubygems has a method for finding this:

C:\>irb -rubygems

> > Gem.user\_home  
> > =\> “C:\Documents and Settings\gthiesfeld”

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/12 "2007-09-25T23:03:10Z")

</div>

Trans wrote:

> I have a little app that needs to store session data. I assume the  
> best place to store it is in one’s home directory, but I need this app  
> to be cross-platform. I glanced through all rbconfig.rb’s  
> Config::CONFIG settings but did not see anything for it. How does one  
> access a cross-platform home directory?

This is one way of guessing:

case RUBY\_PLATFORM  
when /win32/  
ENV[‘APPDATA’] ||  
ENV[‘USERPROFILE’] ||  
ENV[‘HOME’]

else  
ENV[‘HOME’] ||  
File.expand\_path(’~’)  
end

Typical values:

ENV[‘APPDATA’] == “C:\Documents and Settings\username\Application Data”  
ENV[‘USERPROFILE’] == “C:\Documents and Settings\username”

(This is from my ‘preferences’ lib.)

I have no idea what is right on OS X. I’m sure someone has thought this  
through in more detail…

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/13 "2007-09-25T23:04:16Z")

</div>

Joel VanderWerf wrote:

> when /win32/

I think Mac OS X should have the traditional unix variables set, like  
${HOME}  
and ${EDITOR} but I don’t own a mac to find out :’(.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/14 "2007-09-25T23:05:25Z")

</div>

On Sep 15, 2007, at 2:47 AM, Terry P. wrote:

> I think Mac OS X should have the traditional unix variables set,  
> like ${HOME}  
> and ${EDITOR} but I don’t own a mac to find out :’(.

It does. You don’t even have to own one to find out. You can find it  
out in Unix in a Nutshell.  
Those variables are set in any one of the several possible files that  
configure the shell. OS X uses Bash as the default shell.  
(older versions used Tcsh as the default shell, but it’s similar  
enough.)  
It’s as Unix as any \*nix

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/15 "2007-09-25T23:05:30Z")

</div>

Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have no idea what is right on OS X.

ENV[‘HOME’]

for example my “home” is :

/Users/yt

(never tried the File.expand\_path(‘~’) ) BUT on Mac OS X shell ~ expand  
to home.

also i worry about the way win\* representent pathes with \

does that means that, in ruby, there is no platform-independant way to  
representant pathes, as, for example in Java ?

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/16 "2007-09-25T23:05:30Z")

</div>

On Sep 13, 1:37 pm, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Trans wrote:
> 
> > I have a little app that needs to store session data. I assume the  
> > best place to store it is in one’s home directory, but I need this app  
> > to be cross-platform. I glanced through all rbconfig.rb’s  
> > Config::CONFIG settings but did not see anything for it. How does one  
> > access a cross-platform home directory?
> 
> This is one way of guessing:

I think the appdata directory is where you want it for MS Windows. You  
can use win32-dir, since the environment variable may not be defined:

when /mswin/  
require ‘win32/dir’  
Dir::APPDATA

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/17 "2007-09-25T23:04:15Z")

</div>

> -----Original Message-----  
> From: Clifford H. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
> Sent: Saturday, September 15, 2007 6:25 AM  
> To: ruby-talk ML  
> Subject: Re: Cross-platform Home Directory?

> Everyone’s helped you with ways to find the home directory, but on  
> Windows this is not normally the right place to store session data.  
> That’s what Application Data is for. Make a directory under there for your  
> application. Configuration data (user-modifiable but less  
> frequently-changing)  
> may be stored in the registry.

ENV[“APPDATA”] produces the Application Data directory. Best to use  
that:

> > ENV[“APPDATA”]  
> > =\> “C:\Users\CynicalRyan\AppData\Roaming”  
> > exit

Since it is in a different location from Windows XP in Vista (the above  
path  
is a Vista path).

> The Windows certification documents describe where you should put  
> stuff.

Are these available on MSDN?

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/18 "2007-09-25T23:09:59Z")

</div>

Joel VanderWerf wrote:

> when /win32/

I think Mac OS X should have the traditional unix variables set, like  
${HOME}  
and ${EDITOR} but I don’t own a mac to find out :’(.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/cross-platform-home-directory/114647/19 "2007-09-25T23:09:39Z")

</div>

> -----Original Message-----  
> From: “Une Bév” “ue” [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
> Sent: Friday, September 14, 2007 7:20 AM  
> To: ruby-talk ML  
> Subject: Re: Cross-platform Home Directory?
> 
> does that means that, in ruby, there is no platform-independant way to  
> representant pathes, as, for example in Java ?

8:31:09.25 C:\Users\CynicalRyan

> ls gems.txt  
> gems.txt

8:31:14.01 C:\Users\CynicalRyan

> irb --simple-prompt
> 
> > File.open(“c:/Users/CynicalRyan/gems.txt”)  
> > =\> #\<File:c:/Users/CynicalRyan/gems.txt\>

> ruby -v  
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

> ver

Microsoft Windows [Version 6.0.6000]
