Environment variables are variables passed to programs by the command line or the graphical shell. Though there are a number of environment variables that only affect the command line or graphical shell itself (such as PATH or HOME), there are also several that directly affect how Ruby scripts execute.
Accessing Environment Variables from Ruby
Ruby has direct access to environment variables via the ENV hash. Environment variables can be directly read or written to by using the index operator with a string argument. Note that writing to environment variables will only have an effect on child processes of the Ruby script. Other invocations of the script will not see the changes in environment variables.
#!/usr/bin/env ruby
# Print some variables
puts ENV['PATH']
puts ENV['EDITOR']
# Change a variable then launch a new program
ENV['EDITOR'] = 'gedit'
`cheat environment_variables --add`
Passing Environment Variables to Ruby
To pass environment variables to Ruby, simply set that environment variable in the shell. This varies slightly between operating systems, but the concepts remain the same.
To set an environment variable on the Windows command prompt, use the set command.
> set TEST=value
To set an environment variable on Linux or OS X, use the export command. Though environment variables are a normal part of the Bash shell, only variables that have beenexported will be available in programs launched by the Bash shell.
$ export TEST=value
Alternatively, if the environment variable will only be used by the program about to be run, you can define any environment variables before the name of the command. The environment variable will be passed onto the program as its run, but not saved. Any further invocations of the program will not have this environment variable set.
$ EDITOR=gedit cheat environment_variables --add
Environment Variables Used by Ruby
There are a number of environment variables that effect how the Ruby interpreter acts.
-
RUBYOPT - Any command line switches here will be added to any switches specified on the command line.
-
RUBYPATH - When used with the -S switch on the command line, the paths listed inRUBYPATH will be added to the paths searched when looking for Ruby scripts. The paths in RUBYPATH precede the paths listed in PATH.
-
RUBYLIB - The list of paths here will be added to the list of paths Ruby uses to search for libraries included in the program with the require method. The paths in RUBYLIB will be searched before other directories.
相关推荐
"Accessing Variables in Block Objects"这个主题主要探讨了如何在Block内部访问外部变量,这在处理异步操作、回调函数或创建自定义闭包时非常常见。以下是对这一知识点的详细说明: 1. **Block定义与类型**: - ...
- **Ruby Documentation:** Ruby has extensive documentation, and the book provides guidance on accessing and using it to explore the language further. - **RubyGems and Rake:** The book also includes ...
### 使用 JDBC 访问数据库 在现代软件开发中,与数据库进行交互是不可或缺的一部分。Java Database Connectivity (JDBC) 是 Java 平台上的一个标准库,用于与关系型数据库进行通信。通过使用 JDBC API,开发者可以...
本系列练习题围绕“Accessing the WAN”(访问广域网)这一主题展开,涵盖了一系列关于网络连接、路由协议、广域网技术等内容,是CCNA4第四学期的重要学习材料。 一、WAN基础知识 WAN(Wide Area Network)广域网是...
WebAssembly: Accessing C and C++ in Web Applications English | MP4 | AVC 1280×720 | AAC 48KHz 2ch | 1h 45m | 213 MB Accelerate web applications with native code using WebAssembly. WebAssembly is a ...
这些是学习长在学校培训的CCNA的资料 ,希望对你们有用,哈
"accessing-and-modifying.zip"这个压缩包可能包含了关于如何使用Python和OpenCV来访问和修改图像数据的相关资料。接下来,我们将深入探讨这两个关键主题。 Python是一种高级编程语言,以其简洁、易读的语法而闻名...
标题中的"Accessing Twain 2_huntermog_vb.net_"表明这是一个关于使用VB.NET访问TWAIN接口的示例项目。TWAIN是一种标准的软件接口,允许应用程序(如图像扫描器或相机)与硬件设备(如扫描仪)进行通信。在VB.NET...
MCTS Self-Paced Training Kit (Exam 70-516) Accessing Data with Microsoft .NET Framework 4 eBook.pdf.7z. 請用7zip解壓
本主题涉及到的是利用ArcGIS的编程接口进行数据访问和处理,具体案例为"Accessing_Data.rar_ADODr_arcgis_pxa320"。这个压缩包包含了学习资源,帮助用户了解如何通过编程方式连接和操作数据。 1. **ADODB (ActiveX ...
根据提供的标题“Moving and Accessing SAS 9.2”以及描述“Moving and Accessing SAS 9.2 Files”,我们可以明确该文档主要讨论的是如何在不同的操作系统环境中移动和访问SAS 9.2版本的文件。 #### 重要内容概览:...
### Hibernate常见错误解决方案 在开发基于Hibernate框架的应用程序时,我们经常会遇到各种各样的异常情况。这些异常不仅会影响系统的正常运行,还可能增加调试难度。本文将针对几个常见的Hibernate异常进行详细...
The OFS product provides client applications with a group of services (methods) for accessing to variables of a target PLC. OFS is compatible with versions OPC 1.0A and OPC 2.0. It will function with...
### BAPI Development for Accessing SAP Components #### 课程概述 BC417是关于BAPI(Business Application Programming Interface)开发的课程,旨在教授如何通过BAPI来访问SAP组件。该课程提供了深入的理解和...
### 使用Java访问Oracle数据库 #### 引言 在开发Java应用程序时,经常需要与数据库进行交互。对于希望访问Oracle数据库的Java开发者来说,有两种主要的方法:JDBC(Java Database Connectivity)和SQLJ。...
"Accessing Web Services from a Visual Web Application"这一主题主要关注如何在基于Visual Web的应用程序中集成Web服务。Visual Web工具通常提供了直观的界面,使开发者能够轻松地连接到Web服务并使用它们的功能。...
MCTS 70-516 Accessing Data with Microsoft .NET Framework 4 练习1。注意,因为我只能上传小于15兆的文件,所以我把setup1.msi给那出去了。安装时需要把setup1.exe放进Practice Tests文件夹。
在本章"Chapter 3 Accessing the Cloud"中,我们主要探讨了三个核心概念:云计算平台、Web应用程序框架以及Web浏览器。这些知识点是理解云计算访问和应用开发的关键。 首先,让我们详细了解一下**云计算平台**。...
本教程“gs-accessing-data-mysql-master”主要关注如何使用Java语言与MySQL数据库进行交互,这对于开发需要持久化数据的Java应用程序至关重要。 1. **MySQL数据库介绍** - MySQL是一个开源、免费的SQL数据库,...
文档"Oracle Solaris 10 Accessing Third-Party Notice and License Information for Oracle Solaris 10-6"旨在提供对这些第三方组件的通知和许可信息的访问指南,确保用户在使用系统时遵循所有适用的法律和协议。...