[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

method to get (Linux) user home path

pedro mg

2/8/2007 4:05:00 AM

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,

--
pedro mg
ruby 1.8.4 (2005-12-24) [i486-linux]
ubuntu linux edgy-eft 2.6.17-10-386 #2
5 Answers

pedro mg

2/8/2007 7:20:00 AM

0

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

--
pedro mg
ruby 1.8.4 (2005-12-24) [i486-linux]
ubuntu linux edgy-eft 2.6.17-10-386 #2

Martin DeMello

2/8/2007 8:05:00 AM

0

On 2/8/07, pedro mg <_nospam_seti@tquadrado.com> 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

Erik Veenstra

2/8/2007 10:53:00 AM

0

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

I added some examples as well.

gegroet,
Erik V. - http://www.erikve...

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

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}

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


Brian Candler

2/8/2007 12:43:00 PM

0

On Thu, Feb 08, 2007 at 07:52:41PM +0900, Erik Veenstra 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

pedro mg

2/8/2007 11:01:00 PM

0

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

best regards,
--
pedro mg