Module: Env

Defined in:
lib/GGEnv.rb

Overview

Env

Documentation

This file contains auxiliary routines to easily interact with environment variables that contain lists of paths, like PATH, LD_LIBRARY_PATH, MAYA_SCRIPT_PATH, etc.

Usage

Env.check_directories = false       # turn off verification that
                                    # directories exist (default: true)
path = Env['PATH']

path << "C:/newpath"                # As path is modified, so 
                                    # is ENV['PATH']

path.delete_if { |x| x =~ /maya/ }  # remove all paths that have maya

path.unshift ["C:/", "E:/bin"]      # add these paths at start

Env['PATH'] = path[0,2] + path[4,6] # concat two slices and send to PATH

path.check_directories              # check existance of directories
                                    # for this variable only (unlike
                                    # Env.check_directories)

Defined Under Namespace

Classes: EnvVar

Class Method Summary collapse

Class Method Details

.[](a) ⇒ Object

Access an environment variable as an array.

The result is cached internally within the Env module for faster access.

Example

puts Env['LD_LIBRARY_PATH']
path = Env['PATH']
path << ["/usr/local/bin"]
path -= "C:/"


91
92
93
94
# File 'lib/GGEnv.rb', line 91

def self.[](a)
  return @@_envvars[a] if @@_envvars[a]
  @@_envvars[a] = EnvVar.new( a, ENV[a] )
end

.[]=(a, b) ⇒ Object

Assign a new value (Array or String) to an environment variable.

Example

Env['PATH'] = ['/usr/', '/usr/local/']


75
76
77
# File 'lib/GGEnv.rb', line 75

def self.[]=(a, b)
  @@_envvars[a] = EnvVar.new( a, b )
end

.check_directories=(a) ⇒ Object

If true, check directories of all Env variables automatically. Default value is true.

Example

Env.check_directories = false
path = Env['PATH']
path << "/inexistent/path/"
puts path
Env.check_directories = true
puts path


116
117
118
119
120
121
# File 'lib/GGEnv.rb', line 116

def self.check_directories=(a)
  @@_check_dirs = (a == true)
  if @@_check_dirs
    @@_envvars.each_value { |v| v.check_directories }
  end
end

.check_directories?Boolean

Are we checking directories of all variables automatically?

Returns:

  • (Boolean)


99
100
101
# File 'lib/GGEnv.rb', line 99

def self.check_directories?
  @@_check_dirs
end

.clearObject

Clear the Env path cache to free some memory.

Example

path = Env['PATH']
path = nil
Env.clear


63
64
65
66
# File 'lib/GGEnv.rb', line 63

def self.clear
  @@_envvars = {}
  GC.start
end