Class: ENV

Inherits:
Object show all
Defined in:
lib/nuggets/env/user_home.rb

Overview

#

A component of ruby-nuggets, some extensions to the Ruby programming # language. #

#

Copyright © 2008 Jens Wille #

#

Authors: #

Jens Wille <jens.wille@uni-koeln.de>                                    #
                                                                        #

ruby-nuggets is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #

#

ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. #

#

You should have received a copy of the GNU General Public License along # with ruby-nuggets. If not, see <www.gnu.org/licenses/>. #

#

++

Class Method Summary collapse

Class Method Details

.user_encodingObject

call-seq:

ENV.user_encoding => aString or nil

Finds the user’s selected encoding.



39
40
41
42
43
44
45
46
47
# File 'lib/nuggets/env/user_encoding.rb', line 39

def user_encoding
  ENV['ENCODING']          ||
  ENV['LANG'][/\.(.*)/, 1] ||
  if defined?(Win32::Console)
    "CP#{Win32::Console.InputCP}"
  else
    cp = %x{chcp}[/:\s*(.*?)\./, 1] and "CP#{cp}"
  end
end

.user_homeObject

call-seq:

ENV.user_home => aString

Finds the user’s home directory. Stolen from RubyGems ;-)



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nuggets/env/user_home.rb', line 34

def user_home
  %w[HOME USERPROFILE].each { |homekey|
    return ENV[homekey] if ENV[homekey]
  }

  if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
    return "#{ENV['HOMEDRIVE']}:#{ENV['HOMEPATH']}"
  end

  begin
    File.expand_path('~')
  rescue ArgumentError
    File::ALT_SEPARATOR ? 'C:/' : '/'
  end
end