method to get (Linux) user home path

P

pedro mg

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,
 
P

pedro mg

pedro said:
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')
 
M

Martin DeMello

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
 
E

Erik Veenstra

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/

----------------------------------------------------------------

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}

----------------------------------------------------------------
 
B

Brian Candler

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
 
P

pedro mg

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

best regards,
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top