Method to get (Linux) user home path

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

ENV['HOME']

martin

···

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 ?

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}

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

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

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

best regards,

···

--
pedro mg

ruby has the latter included as standard.

require 'tmpdir'
puts Dir.tmpdir

Here's the implementation from tmpdir.rb:

···

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

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

  ##
  # 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