Interacting with Ruby’s runtime environment

In this post I will look at various ways of interacting with a Ruby program’s runtime environment. Following sample program has methods to identify the operating system platform, environment variables in the OS and finding out the command line parameters.

class EnvSample
  # how to detect operating system platform
  # check presence of win32 for Windows, linux for Linux etc.
  def printOSInfo
    puts RUBY_PLATFORM # RUBY_PLATFORM constant contains OS info  
  # how to get enviornment variable in Ruby
  # ENV constant contains a hash of enviornment variables
  def printEnvVariables
    puts ENV.inspect
  # how to get command line parameters in Ruby
  # ARGV constant contains command line parameters
  def printCommandParams
    puts ARGV.inspect # ARGV[0] represents first parameter
  e =
  e.printOSInfo # print OS info
  e.printEnvVariables # print enviornment variables
  e.printCommandParams # print command line parameters

Leave a Reply