Section 14.5. Scripting in Microsoft Windows

14.4. Accessing Environment Variables

Occasionally we need to access environment variables as a link between our program and the outer world. An environment variable is essentially a label referring to a piece of text (typically a small piece); environment variables can be used to store configuration information such as paths, usernames, and so on.

The notion of an environment variable is common in the UNIX world. The Windows world has borrowed it from UNIX (by way of MS-DOS), so the code we show here should run on variants of both Windows and UNIX.

14.4.1. Getting and Setting Environment Variables

The global constant ENV can be used as a hash both for purposes of retrieving and assigning values. In the following code, we retrieve the value of an environment variable. (You would use a semicolon rather than a colon on Windows.)

mypath = ENV["PATH"] # Let's get an array now... dirs = mypath.split(":")

Here's an example of setting a variable. We take the trouble to fork another process to illustrate two facts. First, a child process inherits the environment variables that its parent knows. Second, an environment variable set by a child is not propagated back up to the parent.

ENV["alpha"] = "123" ENV["beta"]  = "456" puts "Parent: alpha = #{ENV['alpha']}" puts "Parent: beta  = #{ENV['beta']}" fork do   # Child code...   x = ENV["alpha"]   ENV["beta"] = "789"   y = ENV["beta"]   puts " Child: alpha = #{x}"   puts " Child: beta  = #{y}" end Process.wait a = ENV["alpha"] b = ENV["beta"] puts "Parent: alpha = #{a}" puts "Parent: beta  = #{b}"

The output here would be the following:

Parent: alpha = 123 Parent: beta  = 456  Child: alpha = 123  Child: beta  = 789 Parent: alpha = 123 Parent: beta  = 456

There is a consequence of the fact that parent processes don't know about their children's variables. Because a Ruby program is typically run in a subshell, any variables changed during execution will not be reflected in the current shell after execution has terminated.

14.4.2. Storing Environment Variables As an Array or Hash

It's important to realize that ENV isn't really a hash; it just looks like one. For example, we can't call the invert method on it; it gives us a NameError because there is no such method. The reason for this implementation is the close tie between the ENV object and the underlying operating system; setting a value has an actual impact on the OS, a behavior that a mere hash can't mimic.

However, we can call the to_hash method to give us a real live hash:

envhash = ENV.to_hash val2var = envhash.invert

Of course, after we have a hash, we can convert it to any other form we prefer (for example, an array):

envarr = ENV.to_hash.to_a

It's not possible to directly reassign a hash to ENV; but we can fake it easily if we need to:

envhash = ENV.to_hash # Manipulate as needed... then assign back. envhash.each {|k,v| ENV[k] = v }

14.4.3. Importing Environment Variables As Globals

There is a small library called importenv.rb, which will run through all the environment variables and import them into the program as global variables. It is used in this way:

require "importenv" # Now our environment variables are all globals... # E.g., $PWD and $LOGNAME where = $PWD who = $LOGNAME puts "In directory #{where}, logged in as #{who}"

Note that because the importenv uses trace_var, the reflection is actually two-way: We can set one of these global variables in our program, and the real environment variable will be set in the same way.

require "importenv" puts "My path is #$PATH" # Prints: /usr/local/bin:/usr/bin:/usr/ucb:/etc:. $PATH = "/ruby-1.8.0:" + $PATH puts "My actual $PATH variable is now #{ENV['PATH']}" # Prints: /ruby-1.8.0:/usr/local/bin:/usr/bin:/usr/ucb:/etc:.

Again we point out that a change in an environment variable within a Ruby program doesn't affect the environment external to the program.

The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: