# Method to get (Linux) user home path

**URL:** <https://www.ruby-forum.com/t/method-to-get-linux-user-home-path/84866>\
**Category:** Ruby\
**Created:** [February 8, 2007, 8:46am UTC](https://www.ruby-forum.com/t/method-to-get-linux-user-home-path/84866 "2007-02-08T08:46:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [February 8, 2007, 8:46am UTC](https://www.ruby-forum.com/t/method-to-get-linux-user-home-path/84866/1 "2007-02-08T08:46:32Z")

</div>

Hi,

i am trying to create a file in ~/.myopts/myopts.yaml but, no go.  
Trying using $HOME/.myopts/myopts.yaml does not work either.  
Only specifying manually /home/me/.myopts/myopts.yaml but that is no  
solution.

Is there any method to get me the path to the user account running the  
script ?

Regards,

---

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [February 8, 2007, 9:05am UTC](https://www.ruby-forum.com/t/method-to-get-linux-user-home-path/84866/2 "2007-02-08T09:05:35Z")

</div>

On 2/8/07, pedro mg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> i am trying to create a file in ~/.myopts/myopts.yaml but, no go.  
> Trying using $HOME/.myopts/myopts.yaml does not work either.  
> Only specifying manually /home/me/.myopts/myopts.yaml but that is no  
> solution.
> 
> Is there any method to get me the path to the user account running the  
> script ?

ENV[‘HOME’]

martin

---

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [February 8, 2007, 11:53am UTC](https://www.ruby-forum.com/t/method-to-get-linux-user-home-path/84866/3 "2007-02-08T11:53:57Z")

</div>

These are my personal implementations of Dir.home and Dir.temp.

I added some examples as well.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

* * *

class Dir  
def self.home(\*args, &block)  
dir = nil

```
 dir ||= ENV["HOME"]
 dir ||= ENV["USERPROFILE"]
 dir ||= "c:/"

 handle_home_and_temp(dir, *args, &block)

```

end

def self.temp(\*args, &block)  
dir = nil

```
 dir ||= ENV["TMPDIR"]
 dir ||= ENV["TMP"]
 dir ||= ENV["TEMP"]
 dir ||= "/tmp"

 handle_home_and_temp(dir, *args, &block)

```

end

private

def self.handle\_home\_and\_temp(dir, \*args, &block)  
file = File.join(\*args)  
file = file.gsub(/\/, “/”)  
file = file.gsub(//+/, “/”)  
file = file.gsub(/^/+/, “”)  
file = file.gsub(//+$/, “”)

```
 dir = dir.gsub(/\\/, "/")
 dir = dir.gsub(/\/+/, "/")
 dir = dir.gsub(/\/+$/, "")
 dir = File.expand_path(file, dir)

 res = dir

 if block
   pdir = Dir.pwd

   Dir.chdir(dir) # Ruby 1.6 doesn't handle Dir.chdir(&block).
     res = block.call(res)
   Dir.chdir(pdir)
 end

 res

```

end  
end

* * *

p Dir.home  
p Dir.home{Dir.pwd}  
p Dir.home(“a”, “b”)  
p Dir.home(“a”, “b”){Dir.pwd}

p Dir.temp  
p Dir.temp{Dir.pwd}  
p Dir.temp(“c”, “d”)  
p Dir.temp(“c”, “d”){Dir.pwd}

---

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [February 8, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/method-to-get-linux-user-home-path/84866/4 "2007-02-08T13:16:08Z")

</div>

pedro mg wrote:

> Is there any method to get me the path to the user account running the  
> script ?

ENV hash has it all…

puts ENV[‘HOME’] if ENV.has\_key?(‘HOME’)

---

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [February 9, 2007, 12:36am UTC](https://www.ruby-forum.com/t/method-to-get-linux-user-home-path/84866/5 "2007-02-09T00:36:31Z")

</div>

Thank you, Martin, Erik and Brian.  
Very useful information, all 3 of you posted.

best regards,

---

<div class="post-metadata">

**Author:** ![pedro\_mg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c2a13f/32.png) [@pedro\_mg](https://www.ruby-forum.com/u/pedro_mg)\
**Post date:** [February 8, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/method-to-get-linux-user-home-path/84866/6 "2007-02-08T13:44:13Z")

</div>

On Thu, Feb 08, 2007 at 07:52:41PM +0900, Erik V. wrote:

> These are my personal implementations of Dir.home and Dir.temp.

ruby has the latter included as standard.

require ‘tmpdir’  
puts Dir.tmpdir

Here’s the implementation from tmpdir.rb:

## 

# Returns the operating system’s temporary file path.

def Dir::tmpdir  
tmp = ‘.’  
if $SAFE \> 0  
tmp = @@systmpdir  
else  
for dir in [ENV[‘TMPDIR’], ENV[‘TMP’], ENV[‘TEMP’],  
ENV[‘USERPROFILE’], @@systmpdir, ‘/tmp’]  
if dir and File.directory?(dir) and File.writable?(dir)  
tmp = dir  
break  
end  
end  
end  
File.expand\_path(tmp)  
end
