class ENV
ENV 是用于访问环境变量的类似哈希的对象。
与操作系统的交互¶ ↑
ENV 对象与操作系统的环境变量进行交互
-
当您在
ENV中获取名称的值时,该值是从当前环境变量中检索的。 -
当您在
ENV中创建或设置名称-值对时,名称和值会立即设置在环境变量中。 -
当您在
ENV中删除名称-值对时,它会立即从环境变量中删除。
名称和值¶ ↑
通常,名称或值是一个 String。
有效的名称和值¶ ↑
每个名称或值必须是以下之一
无效的名称和值¶ ↑
一个新名称
-
不能为空字符串
ENV[''] = '0' # Raises Errno::EINVAL (Invalid argument - ruby_setenv())
-
不能包含字符
"="ENV['='] = '0' # Raises Errno::EINVAL (Invalid argument - ruby_setenv(=))
一个新名称或值
-
不能是非 String 且不响应 #to_str 的对象
ENV['foo'] = Object.new # Raises TypeError (no implicit conversion of Object into String) ENV[Object.new] = '0' # Raises TypeError (no implicit conversion of Object into String)
-
不能包含 NUL 字符
"\0"ENV['foo'] = "\0" # Raises ArgumentError (bad environment variable value: contains null byte) ENV["\0"] == '0' # Raises ArgumentError (bad environment variable name: contains null byte)
-
不能具有与 ASCII 不兼容的编码,例如 UTF-16LE 或 ISO-2022-JP
ENV['foo'] = '0'.force_encoding(Encoding::ISO_2022_JP) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: ISO-2022-JP) ENV["foo".force_encoding(Encoding::ISO_2022_JP)] = '0' # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: ISO-2022-JP)
关于排序¶ ↑
ENV 按照操作系统环境变量中找到的顺序枚举其名称/值对。因此,ENV 内容的顺序取决于操作系统,并且可能是不确定的。
这将在以下方面看到
-
由
ENV方法返回的Enumerator。 -
由
ENV.keys、ENV.values或ENV.to_a返回的Array。 -
由
ENV.inspect返回的String。 -
由
ENV.key返回的名称。
关于示例¶ ↑
ENV 中的某些方法返回 ENV 本身。通常,存在许多环境变量。在这里的示例中显示一个大的 ENV 没有用,因此大多数示例代码片段都以重置 ENV 的内容开始
-
ENV.replace用一组新的条目替换ENV。
这里有什么¶ ↑
首先,看看其他地方有什么。类 ENV
-
继承自 class Object。
-
扩展了 模块 Enumerable,
在这里,类 ENV 提供了对以下方面有用的方法
用于查询的方法¶ ↑
-
::[]:如果存在,则返回给定环境变量名称的值 -
::has_value?、::value?:返回给定值是否在ENV中。 -
::include?、::has_key?、::key?、::member?:返回给定名称是否在ENV中。 -
::key:返回具有给定值的第一个条目的名称。 -
::value?:返回是否有任何条目具有给定值。
用于赋值的方法¶ ↑
用于删除的方法¶ ↑
-
::delete:如果存在,则删除指定的环境变量名称。 -
::delete_if:删除由块选择的条目。 -
::keep_if:删除未由块选择的条目。 -
::reject!:与 delete_if 类似,但如果没有进行任何更改,则返回nil。 -
::shift:删除并返回第一个条目。
用于迭代的方法¶ ↑
-
::each、::each_pair:使用每个名称/值对调用该块。 -
::each_key:使用每个名称调用该块。 -
::each_value:使用每个值调用该块。
用于转换的方法¶ ↑
-
::assoc:如果存在,则返回包含指定环境变量名称和值的 2 元素数组 -
::except:返回除给定值之外的所有名称/值对的哈希。 -
::fetch:返回给定名称的值。 -
::keys:返回所有名称的数组。 -
::rassoc:返回第一个找到的具有给定值的条目的名称和值。 -
::reject:返回由块未拒绝的那些条目的哈希。 -
::slice:返回给定名称及其对应值的哈希。 -
::to_a:以 2 元素数组的数组形式返回条目。 -
::to_h:返回由块选择的条目的哈希。 -
::to_hash:返回所有条目的哈希。 -
::to_s:返回字符串'ENV'。 -
::values:以数组形式返回所有值。 -
::values_at:返回给定名称的值的数组。
更多方法¶ ↑
公共类方法
来源
static VALUE
rb_f_getenv(VALUE obj, VALUE name)
{
const char *nam = env_name(name);
VALUE env = getenv_with_lock(nam);
return env;
}
如果存在,则返回环境变量 name 的值
ENV['foo'] = '0' ENV['foo'] # => "0"
如果指定变量不存在,则返回 nil。
如果 name 无效,则引发异常。请参阅 无效的名称和值。
来源
static VALUE
env_aset_m(VALUE obj, VALUE nm, VALUE val)
{
return env_aset(nm, val);
}
创建、更新或删除指定的环境变量,并返回该值。name 和 value 都可以是 String 的实例。请参阅 有效的名称和值。
-
如果指定的环境变量不存在
-
如果
value为nil,则不执行任何操作。ENV.clear ENV['foo'] = nil # => nil ENV.include?('foo') # => false ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
-
如果
value不为nil,则使用name和value创建环境变量# Create 'foo' using ENV.[]=. ENV['foo'] = '0' # => '0' ENV['foo'] # => '0' # Create 'bar' using ENV.store. ENV.store('bar', '1') # => '1' ENV['bar'] # => '1'
-
-
如果指定的环境变量存在
-
如果
value不为nil,则使用值value更新环境变量# Update 'foo' using ENV.[]=. ENV['foo'] = '2' # => '2' ENV['foo'] # => '2' # Update 'bar' using ENV.store. ENV.store('bar', '3') # => '3' ENV['bar'] # => '3'
-
如果
value为nil,则删除环境变量# Delete 'foo' using ENV.[]=. ENV['foo'] = nil # => nil ENV.include?('foo') # => false # Delete 'bar' using ENV.store. ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
-
如果 name 或 value 无效,则引发异常。请参阅 无效的名称和值。
来源
static VALUE
env_assoc(VALUE env, VALUE key)
{
const char *s = env_name(key);
VALUE e = getenv_with_lock(s);
if (!NIL_P(e)) {
return rb_assoc_new(key, e);
}
else {
return Qnil;
}
}
返回一个 2 元素的 Array,其中包含环境变量 name 的名称和值 (如果存在)
ENV.replace('foo' => '0', 'bar' => '1') ENV.assoc('foo') # => ['foo', '0']
如果 name 是有效的 String 并且不存在这样的环境变量,则返回 nil。
如果 name 是空 String 或包含字符 '=' 的 String,则返回 nil。
如果 name 是包含 NUL 字符 "\0" 的 String,则引发异常
ENV.assoc("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
如果 name 具有与 ASCII 不兼容的编码,则引发异常
ENV.assoc("\xa1\xa1".force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
如果 name 不是 String,则引发异常
ENV.assoc(Object.new) # TypeError (no implicit conversion of Object into String)
来源
static VALUE
env_clear(VALUE _)
{
return rb_env_clear();
}
删除每个环境变量;返回 ENV
ENV.replace('foo' => '0', 'bar' => '1') ENV.size # => 2 ENV.clear # => ENV ENV.size # => 0
来源
static VALUE
env_clone(int argc, VALUE *argv, VALUE obj)
{
if (argc) {
VALUE opt;
if (rb_scan_args(argc, argv, "0:", &opt) < argc) {
rb_get_freeze_opt(1, &opt);
}
}
rb_raise(rb_eTypeError, "Cannot clone ENV, use ENV.to_h to get a copy of ENV as a hash");
}
抛出 TypeError 错误,因为 ENV 是进程级环境变量的包装器,克隆没有意义。使用 to_h 获取 ENV 数据的哈希副本。
来源
static VALUE
env_delete_m(VALUE obj, VALUE name)
{
VALUE val;
val = env_delete(name);
if (NIL_P(val) && rb_block_given_p()) val = rb_yield(name);
return val;
}
删除具有 name 的环境变量(如果存在)并返回其值
ENV['foo'] = '0' ENV.delete('foo') # => '0'
如果没有给出代码块,且指定名称的环境变量不存在,则返回 nil。
如果给出了代码块,且环境变量不存在,则将 name 传递给代码块,并返回代码块的值
ENV.delete('foo') { |name| name * 2 } # => "foofoo"
如果给出了代码块,且环境变量存在,则删除该环境变量并返回其值(忽略代码块)
ENV['foo'] = '0' ENV.delete('foo') { |name| raise 'ignored' } # => "0"
如果 name 无效,则引发异常。请参阅 无效的名称和值。
来源
static VALUE
env_delete_if(VALUE ehash)
{
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
env_reject_bang(ehash);
return envtbl;
}
将每个环境变量的名称及其值作为包含 2 个元素的 Array 传递给代码块,删除代码块返回真值的每个环境变量,并返回 ENV(无论是否进行删除)
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.delete_if { |name, value| name.start_with?('b') } # => ENV ENV # => {"foo"=>"0"} ENV.delete_if { |name, value| name.start_with?('b') } # => ENV
如果没有给出代码块,则返回 Enumerator
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.delete_if # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:delete_if!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"foo"=>"0"} e.each { |name, value| name.start_with?('b') } # => ENV
来源
来源
static VALUE
env_each_pair(VALUE ehash)
{
long i;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
VALUE ary = rb_ary_new();
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s) {
rb_ary_push(ary, env_str_new(*env, s-*env));
rb_ary_push(ary, env_str_new2(s+1));
}
env++;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
if (rb_block_pair_yield_optimizable()) {
for (i=0; i<RARRAY_LEN(ary); i+=2) {
rb_yield_values(2, RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1));
}
}
else {
for (i=0; i<RARRAY_LEN(ary); i+=2) {
rb_yield(rb_assoc_new(RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1)));
}
}
return ehash;
}
将每个环境变量的名称及其值作为包含 2 个元素的数组传递给代码块
h = {} ENV.each_pair { |name, value| h[name] = value } # => ENV h # => {"bar"=>"1", "foo"=>"0"}
如果没有给出代码块,则返回 Enumerator
h = {} e = ENV.each_pair # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_pair> e.each { |name, value| h[name] = value } # => ENV h # => {"bar"=>"1", "foo"=>"0"}
来源
static VALUE
env_each_key(VALUE ehash)
{
VALUE keys;
long i;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
keys = env_keys(FALSE);
for (i=0; i<RARRAY_LEN(keys); i++) {
rb_yield(RARRAY_AREF(keys, i));
}
return ehash;
}
将每个环境变量的名称传递给代码块
ENV.replace('foo' => '0', 'bar' => '1') # => ENV names = [] ENV.each_key { |name| names.push(name) } # => ENV names # => ["bar", "foo"]
如果没有给出代码块,则返回 Enumerator
e = ENV.each_key # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_key> names = [] e.each { |name| names.push(name) } # => ENV names # => ["bar", "foo"]
来源
static VALUE
env_each_pair(VALUE ehash)
{
long i;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
VALUE ary = rb_ary_new();
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s) {
rb_ary_push(ary, env_str_new(*env, s-*env));
rb_ary_push(ary, env_str_new2(s+1));
}
env++;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
if (rb_block_pair_yield_optimizable()) {
for (i=0; i<RARRAY_LEN(ary); i+=2) {
rb_yield_values(2, RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1));
}
}
else {
for (i=0; i<RARRAY_LEN(ary); i+=2) {
rb_yield(rb_assoc_new(RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1)));
}
}
return ehash;
}
将每个环境变量的名称及其值作为包含 2 个元素的数组传递给代码块
h = {} ENV.each_pair { |name, value| h[name] = value } # => ENV h # => {"bar"=>"1", "foo"=>"0"}
如果没有给出代码块,则返回 Enumerator
h = {} e = ENV.each_pair # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_pair> e.each { |name, value| h[name] = value } # => ENV h # => {"bar"=>"1", "foo"=>"0"}
来源
static VALUE
env_each_value(VALUE ehash)
{
VALUE values;
long i;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
values = env_values();
for (i=0; i<RARRAY_LEN(values); i++) {
rb_yield(RARRAY_AREF(values, i));
}
return ehash;
}
将每个环境变量的值传递给代码块
ENV.replace('foo' => '0', 'bar' => '1') # => ENV values = [] ENV.each_value { |value| values.push(value) } # => ENV values # => ["1", "0"]
如果没有给出代码块,则返回 Enumerator
e = ENV.each_value # => #<Enumerator: {"bar"=>"1", "foo"=>"0"}:each_value> values = [] e.each { |value| values.push(value) } # => ENV values # => ["1", "0"]
来源
static VALUE
env_empty_p(VALUE _)
{
bool empty = true;
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
if (env[0] != 0) {
empty = false;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
return RBOOL(empty);
}
当没有环境变量时返回 true,否则返回 false
ENV.clear ENV.empty? # => true ENV['foo'] = '0' ENV.empty? # => false
来源
static VALUE
env_except(int argc, VALUE *argv, VALUE _)
{
int i;
VALUE key, hash = env_to_hash();
for (i = 0; i < argc; i++) {
key = argv[i];
rb_hash_delete(hash, key);
}
return hash;
}
返回一个哈希,其中不包含 ENV 中给定的键及其值。
ENV #=> {"LANG"=>"en_US.UTF-8", "TERM"=>"xterm-256color", "HOME"=>"/Users/rhc"} ENV.except("TERM","HOME") #=> {"LANG"=>"en_US.UTF-8"}
来源
static VALUE
env_fetch(int argc, VALUE *argv, VALUE _)
{
VALUE key;
long block_given;
const char *nam;
VALUE env;
rb_check_arity(argc, 1, 2);
key = argv[0];
block_given = rb_block_given_p();
if (block_given && argc == 2) {
rb_warn("block supersedes default value argument");
}
nam = env_name(key);
env = getenv_with_lock(nam);
if (NIL_P(env)) {
if (block_given) return rb_yield(key);
if (argc == 1) {
rb_key_err_raise(rb_sprintf("key not found: \"%"PRIsVALUE"\"", key), envtbl, key);
}
return argv[1];
}
return env;
}
如果 name 是环境变量的名称,则返回其值
ENV['foo'] = '0' ENV.fetch('foo') # => '0'
否则,如果给出了代码块(但没有默认值),则将 name 传递给代码块并返回代码块的返回值
ENV.fetch('foo') { |name| :need_not_return_a_string } # => :need_not_return_a_string
否则,如果给出了默认值(但没有代码块),则返回默认值
ENV.delete('foo') ENV.fetch('foo', :default_need_not_be_a_string) # => :default_need_not_be_a_string
如果环境变量不存在,且同时给出了默认值和代码块,则发出警告(“warning: block supersedes default value argument”),将 name 传递给代码块,并返回代码块的返回值
ENV.fetch('foo', :default) { |name| :block_return } # => :block_return
如果 name 有效但未找到,并且没有给出默认值或代码块,则抛出 KeyError 错误
ENV.fetch('foo') # Raises KeyError (key not found: "foo")
如果 name 无效,则引发异常。请参阅 无效的名称和值。
来源
static VALUE
env_select(VALUE ehash)
{
VALUE result;
VALUE keys;
long i;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
result = rb_hash_new();
keys = env_keys(FALSE);
for (i = 0; i < RARRAY_LEN(keys); ++i) {
VALUE key = RARRAY_AREF(keys, i);
VALUE val = rb_f_getenv(Qnil, key);
if (!NIL_P(val)) {
if (RTEST(rb_yield_values(2, key, val))) {
rb_hash_aset(result, key, val);
}
}
}
RB_GC_GUARD(keys);
return result;
}
将每个环境变量的名称及其值作为包含 2 个元素的 Array 传递给代码块,返回一个 Hash,其中包含代码块返回真值的名称和值
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select { |name, value| name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"} ENV.filter { |name, value| name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
如果没有给出代码块,则返回 Enumerator
e = ENV.select # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:select> e.each { |name, value | name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"} e = ENV.filter # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:filter> e.each { |name, value | name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
来源
static VALUE
env_select_bang(VALUE ehash)
{
VALUE keys;
long i;
int del = 0;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
keys = env_keys(FALSE);
RBASIC_CLEAR_CLASS(keys);
for (i=0; i<RARRAY_LEN(keys); i++) {
VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
if (!NIL_P(val)) {
if (!RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) {
env_delete(RARRAY_AREF(keys, i));
del++;
}
}
}
RB_GC_GUARD(keys);
if (del == 0) return Qnil;
return envtbl;
}
将每个环境变量的名称及其值作为包含 2 个元素的 Array 传递给代码块,删除代码块返回 false 或 nil 的每个条目,如果进行了任何删除,则返回 ENV,否则返回 nil
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select! { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} ENV.select! { |name, value| true } # => nil ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.filter! { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} ENV.filter! { |name, value| true } # => nil
如果没有给出代码块,则返回 Enumerator
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.select! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:select!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} e.each { |name, value| true } # => nil ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.filter! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:filter!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} e.each { |name, value| true } # => nil
来源
static VALUE
env_freeze(VALUE self)
{
rb_raise(rb_eTypeError, "cannot freeze ENV");
UNREACHABLE_RETURN(self);
}
抛出异常
ENV.freeze # Raises TypeError (cannot freeze ENV)
来源
static VALUE
env_has_key(VALUE env, VALUE key)
{
const char *s = env_name(key);
return RBOOL(has_env_with_lock(s));
}
如果存在具有给定 name 的环境变量,则返回 true
ENV.replace('foo' => '0', 'bar' => '1') ENV.include?('foo') # => true
如果 name 是有效的 String 并且没有这样的环境变量,则返回 false
ENV.include?('baz') # => false
如果 name 是空 String 或包含字符 '=' 的 String,则返回 false
ENV.include?('') # => false ENV.include?('=') # => false
如果 name 是包含 NUL 字符 "\0" 的 String,则引发异常
ENV.include?("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
如果 name 具有与 ASCII 不兼容的编码,则引发异常
ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
如果 name 不是 String,则引发异常
ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
来源
static VALUE
env_has_value(VALUE dmy, VALUE obj)
{
obj = rb_check_string_type(obj);
if (NIL_P(obj)) return Qnil;
VALUE ret = Qfalse;
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s++) {
long len = strlen(s);
if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
ret = Qtrue;
break;
}
}
env++;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
return ret;
}
如果 value 是某个环境变量名称的值,则返回 true,否则返回 false
ENV.replace('foo' => '0', 'bar' => '1') ENV.value?('0') # => true ENV.has_value?('0') # => true ENV.value?('2') # => false ENV.has_value?('2') # => false
来源
static VALUE
env_has_key(VALUE env, VALUE key)
{
const char *s = env_name(key);
return RBOOL(has_env_with_lock(s));
}
如果存在具有给定 name 的环境变量,则返回 true
ENV.replace('foo' => '0', 'bar' => '1') ENV.include?('foo') # => true
如果 name 是有效的 String 并且没有这样的环境变量,则返回 false
ENV.include?('baz') # => false
如果 name 是空 String 或包含字符 '=' 的 String,则返回 false
ENV.include?('') # => false ENV.include?('=') # => false
如果 name 是包含 NUL 字符 "\0" 的 String,则引发异常
ENV.include?("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
如果 name 具有与 ASCII 不兼容的编码,则引发异常
ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
如果 name 不是 String,则引发异常
ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
来源
static VALUE
env_inspect(VALUE _)
{
VALUE i;
VALUE str = rb_str_buf_new2("{");
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (env != environ) {
rb_str_buf_cat2(str, ", ");
}
if (s) {
rb_str_buf_cat2(str, "\"");
rb_str_buf_cat(str, *env, s-*env);
rb_str_buf_cat2(str, "\"=>");
i = rb_inspect(rb_str_new2(s+1));
rb_str_buf_append(str, i);
}
env++;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
rb_str_buf_cat2(str, "}");
return str;
}
将环境变量的内容作为字符串返回
ENV.replace('foo' => '0', 'bar' => '1') ENV.inspect # => "{\"bar\"=>\"1\", \"foo\"=>\"0\"}"
来源
来源
static VALUE
env_keep_if(VALUE ehash)
{
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
env_select_bang(ehash);
return envtbl;
}
将每个环境变量的名称及其值作为包含 2 个元素的 Array 传递给代码块,删除代码块返回 false 或 nil 的每个环境变量,并返回 ENV
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.keep_if { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"}
如果没有给出代码块,则返回 Enumerator
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.keep_if # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:keep_if> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"}
来源
static VALUE
env_key(VALUE dmy, VALUE value)
{
StringValue(value);
VALUE str = Qnil;
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s++) {
long len = strlen(s);
if (RSTRING_LEN(value) == len && strncmp(s, RSTRING_PTR(value), len) == 0) {
str = env_str_new(*env, s-*env-1);
break;
}
}
env++;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
return str;
}
返回具有 value 的第一个环境变量的名称(如果存在)
ENV.replace('foo' => '0', 'bar' => '0') ENV.key('0') # => "foo"
检查环境变量的顺序取决于操作系统。请参阅 关于排序。
如果没有这样的值,则返回 nil。
如果 value 无效,则抛出异常
ENV.key(Object.new) # raises TypeError (no implicit conversion of Object into String)
请参阅 无效的名称和值。
来源
static VALUE
env_has_key(VALUE env, VALUE key)
{
const char *s = env_name(key);
return RBOOL(has_env_with_lock(s));
}
如果存在具有给定 name 的环境变量,则返回 true
ENV.replace('foo' => '0', 'bar' => '1') ENV.include?('foo') # => true
如果 name 是有效的 String 并且没有这样的环境变量,则返回 false
ENV.include?('baz') # => false
如果 name 是空 String 或包含字符 '=' 的 String,则返回 false
ENV.include?('') # => false ENV.include?('=') # => false
如果 name 是包含 NUL 字符 "\0" 的 String,则引发异常
ENV.include?("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
如果 name 具有与 ASCII 不兼容的编码,则引发异常
ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
如果 name 不是 String,则引发异常
ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
来源
来源
static VALUE
env_size(VALUE _)
{
return INT2FIX(env_size_with_lock());
}
返回环境变量的计数
ENV.replace('foo' => '0', 'bar' => '1') ENV.length # => 2 ENV.size # => 2
来源
static VALUE
env_has_key(VALUE env, VALUE key)
{
const char *s = env_name(key);
return RBOOL(has_env_with_lock(s));
}
如果存在具有给定 name 的环境变量,则返回 true
ENV.replace('foo' => '0', 'bar' => '1') ENV.include?('foo') # => true
如果 name 是有效的 String 并且没有这样的环境变量,则返回 false
ENV.include?('baz') # => false
如果 name 是空 String 或包含字符 '=' 的 String,则返回 false
ENV.include?('') # => false ENV.include?('=') # => false
如果 name 是包含 NUL 字符 "\0" 的 String,则引发异常
ENV.include?("\0") # Raises ArgumentError (bad environment variable name: contains null byte)
如果 name 具有与 ASCII 不兼容的编码,则引发异常
ENV.include?("\xa1\xa1".force_encoding(Encoding::UTF_16LE)) # Raises ArgumentError (bad environment variable name: ASCII incompatible encoding: UTF-16LE)
如果 name 不是 String,则引发异常
ENV.include?(Object.new) # TypeError (no implicit conversion of Object into String)
来源
static VALUE
env_update(int argc, VALUE *argv, VALUE env)
{
rb_foreach_func *func = rb_block_given_p() ?
env_update_block_i : env_update_i;
for (int i = 0; i < argc; ++i) {
VALUE hash = argv[i];
if (env == hash) continue;
hash = to_hash(hash);
rb_hash_foreach(hash, func, 0);
}
return env;
}
将给定 hash 中的每个键/值对添加到 ENV;返回 ENV
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('baz' => '2', 'bat' => '3') # => {"bar"=>"1", "bat"=>"3", "baz"=>"2", "foo"=>"0"}
删除值为 nil 的哈希对应的 ENV 条目
ENV.merge!('baz' => nil, 'bat' => nil) # => {"bar"=>"1", "foo"=>"0"}
对于已存在的名称,如果没有给出代码块,则覆盖 ENV 值
ENV.merge!('foo' => '4') # => {"bar"=>"1", "foo"=>"4"}
对于已存在的名称,如果给出了代码块,则将名称、其 ENV 值及其哈希值传递给代码块;代码块的返回值将成为新名称
ENV.merge!('foo' => '5') { |name, env_val, hash_val | env_val + hash_val } # => {"bar"=>"1", "foo"=>"45"}
如果名称或值无效,则抛出异常(请参阅 无效的名称和值);
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('foo' => '6', :bar => '7', 'baz' => '9') # Raises TypeError (no implicit conversion of Symbol into String) ENV # => {"bar"=>"1", "foo"=>"6"} ENV.merge!('foo' => '7', 'bar' => 8, 'baz' => '9') # Raises TypeError (no implicit conversion of Integer into String) ENV # => {"bar"=>"1", "foo"=>"7"}
如果代码块返回无效名称,则抛出异常:(请参阅 无效的名称和值)
ENV.merge!('bat' => '8', 'foo' => '9') { |name, env_val, hash_val | 10 } # Raises TypeError (no implicit conversion of Integer into String) ENV # => {"bar"=>"1", "bat"=>"8", "foo"=>"7"}
请注意,对于上述异常,无效名称或值之前的哈希对会正常处理;之后的哈希对会被忽略。
来源
static VALUE
env_rassoc(VALUE dmy, VALUE obj)
{
obj = rb_check_string_type(obj);
if (NIL_P(obj)) return Qnil;
VALUE result = Qnil;
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
while (*env) {
const char *p = *env;
char *s = strchr(p, '=');
if (s++) {
long len = strlen(s);
if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
result = rb_assoc_new(rb_str_new(p, s-p-1), obj);
break;
}
}
env++;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
return result;
}
返回一个包含 2 个元素的 Array,其中包含具有值 value 的第一个找到的环境变量的名称和值(如果存在)
ENV.replace('foo' => '0', 'bar' => '0') ENV.rassoc('0') # => ["bar", "0"]
检查环境变量的顺序取决于操作系统。请参阅 关于排序。
如果没有这样的环境变量,则返回 nil。
来源
static VALUE
env_reject(VALUE _)
{
return rb_hash_delete_if(env_to_hash());
}
将每个环境变量的名称及其值作为包含 2 个元素的 Array 传递给代码块。返回一个 Hash,其项由代码块确定。当代码块返回真值时,将名称/值对添加到返回的 Hash;否则忽略该对
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.reject { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
如果没有给出代码块,则返回 Enumerator
e = ENV.reject e.each { |name, value| name.start_with?('b') } # => {"foo"=>"0"}
来源
static VALUE
env_reject_bang(VALUE ehash)
{
VALUE keys;
long i;
int del = 0;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
keys = env_keys(FALSE);
RBASIC_CLEAR_CLASS(keys);
for (i=0; i<RARRAY_LEN(keys); i++) {
VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
if (!NIL_P(val)) {
if (RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) {
env_delete(RARRAY_AREF(keys, i));
del++;
}
}
}
RB_GC_GUARD(keys);
if (del == 0) return Qnil;
return envtbl;
}
与 ENV.delete_if 类似,但如果没有进行任何更改,则返回 nil。
将每个环境变量的名称及其值作为包含 2 个元素的 Array 传递给代码块,删除代码块返回真值的每个环境变量,并返回 ENV(如果进行了任何删除),否则返回 nil(如果没有进行删除)
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.reject! { |name, value| name.start_with?('b') } # => ENV ENV # => {"foo"=>"0"} ENV.reject! { |name, value| name.start_with?('b') } # => nil
如果没有给出代码块,则返回 Enumerator
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.reject! # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:reject!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"foo"=>"0"} e.each { |name, value| name.start_with?('b') } # => nil
来源
static VALUE
env_replace(VALUE env, VALUE hash)
{
VALUE keys;
long i;
keys = env_keys(TRUE);
if (env == hash) return env;
hash = to_hash(hash);
rb_hash_foreach(hash, env_replace_i, keys);
for (i=0; i<RARRAY_LEN(keys); i++) {
env_delete(RARRAY_AREF(keys, i));
}
RB_GC_GUARD(keys);
return env;
}
使用给定 hash 中的名称/值对替换环境变量的全部内容;返回 ENV。
使用给定对替换 ENV 的内容
ENV.replace('foo' => '0', 'bar' => '1') # => ENV ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
如果名称或值无效,则抛出异常(请参阅 无效的名称和值)
ENV.replace('foo' => '0', :bar => '1') # Raises TypeError (no implicit conversion of Symbol into String) ENV.replace('foo' => '0', 'bar' => 1) # Raises TypeError (no implicit conversion of Integer into String) ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
来源
static VALUE
env_select(VALUE ehash)
{
VALUE result;
VALUE keys;
long i;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
result = rb_hash_new();
keys = env_keys(FALSE);
for (i = 0; i < RARRAY_LEN(keys); ++i) {
VALUE key = RARRAY_AREF(keys, i);
VALUE val = rb_f_getenv(Qnil, key);
if (!NIL_P(val)) {
if (RTEST(rb_yield_values(2, key, val))) {
rb_hash_aset(result, key, val);
}
}
}
RB_GC_GUARD(keys);
return result;
}
将每个环境变量的名称及其值作为包含 2 个元素的 Array 传递给代码块,返回一个 Hash,其中包含代码块返回真值的名称和值
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select { |name, value| name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"} ENV.filter { |name, value| name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
如果没有给出代码块,则返回 Enumerator
e = ENV.select # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:select> e.each { |name, value | name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"} e = ENV.filter # => #<Enumerator: {"bar"=>"1", "baz"=>"2", "foo"=>"0"}:filter> e.each { |name, value | name.start_with?('b') } # => {"bar"=>"1", "baz"=>"2"}
来源
static VALUE
env_select_bang(VALUE ehash)
{
VALUE keys;
long i;
int del = 0;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
keys = env_keys(FALSE);
RBASIC_CLEAR_CLASS(keys);
for (i=0; i<RARRAY_LEN(keys); i++) {
VALUE val = rb_f_getenv(Qnil, RARRAY_AREF(keys, i));
if (!NIL_P(val)) {
if (!RTEST(rb_yield_values(2, RARRAY_AREF(keys, i), val))) {
env_delete(RARRAY_AREF(keys, i));
del++;
}
}
}
RB_GC_GUARD(keys);
if (del == 0) return Qnil;
return envtbl;
}
将每个环境变量的名称及其值作为包含 2 个元素的 Array 传递给代码块,删除代码块返回 false 或 nil 的每个条目,如果进行了任何删除,则返回 ENV,否则返回 nil
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.select! { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} ENV.select! { |name, value| true } # => nil ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.filter! { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} ENV.filter! { |name, value| true } # => nil
如果没有给出代码块,则返回 Enumerator
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.select! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:select!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} e.each { |name, value| true } # => nil ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') e = ENV.filter! # => #<Enumerator: {"bar"=>"1", "baz"=>"2"}:filter!> e.each { |name, value| name.start_with?('b') } # => ENV ENV # => {"bar"=>"1", "baz"=>"2"} e.each { |name, value| true } # => nil
来源
static VALUE
env_shift(VALUE _)
{
VALUE result = Qnil;
VALUE key = Qnil;
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
if (*env) {
const char *p = *env;
char *s = strchr(p, '=');
if (s) {
key = env_str_new(p, s-p);
VALUE val = env_str_new2(getenv(RSTRING_PTR(key)));
result = rb_assoc_new(key, val);
}
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
if (!NIL_P(key)) {
env_delete(key);
}
return result;
}
从 ENV 中删除第一个环境变量,并返回一个包含其名称和值的包含 2 个元素的 Array
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_hash # => {'bar' => '1', 'foo' => '0'} ENV.shift # => ['bar', '1'] ENV.to_hash # => {'foo' => '0'}
哪个环境变量是“第一个”取决于操作系统。请参阅 关于排序。
如果环境变量为空,则返回 nil。
来源
static VALUE
env_size(VALUE _)
{
return INT2FIX(env_size_with_lock());
}
返回环境变量的计数
ENV.replace('foo' => '0', 'bar' => '1') ENV.length # => 2 ENV.size # => 2
来源
static VALUE
env_slice(int argc, VALUE *argv, VALUE _)
{
int i;
VALUE key, value, result;
if (argc == 0) {
return rb_hash_new();
}
result = rb_hash_new_with_size(argc);
for (i = 0; i < argc; i++) {
key = argv[i];
value = rb_f_getenv(Qnil, key);
if (value != Qnil)
rb_hash_aset(result, key, value);
}
return result;
}
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2', 'bat' => '3') ENV.slice('foo', 'baz') # => {"foo"=>"0", "baz"=>"2"} ENV.slice('baz', 'foo') # => {"baz"=>"2", "foo"=>"0"}
如果任何 names 无效,则抛出异常(请参阅 无效的名称和值)
ENV.slice('foo', 'bar', :bat) # Raises TypeError (no implicit conversion of Symbol into String)
来源
static VALUE
env_aset_m(VALUE obj, VALUE nm, VALUE val)
{
return env_aset(nm, val);
}
创建、更新或删除指定的环境变量,并返回该值。name 和 value 都可以是 String 的实例。请参阅 有效的名称和值。
-
如果指定的环境变量不存在
-
如果
value为nil,则不执行任何操作。ENV.clear ENV['foo'] = nil # => nil ENV.include?('foo') # => false ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
-
如果
value不为nil,则使用name和value创建环境变量# Create 'foo' using ENV.[]=. ENV['foo'] = '0' # => '0' ENV['foo'] # => '0' # Create 'bar' using ENV.store. ENV.store('bar', '1') # => '1' ENV['bar'] # => '1'
-
-
如果指定的环境变量存在
-
如果
value不为nil,则使用值value更新环境变量# Update 'foo' using ENV.[]=. ENV['foo'] = '2' # => '2' ENV['foo'] # => '2' # Update 'bar' using ENV.store. ENV.store('bar', '3') # => '3' ENV['bar'] # => '3'
-
如果
value为nil,则删除环境变量# Delete 'foo' using ENV.[]=. ENV['foo'] = nil # => nil ENV.include?('foo') # => false # Delete 'bar' using ENV.store. ENV.store('bar', nil) # => nil ENV.include?('bar') # => false
-
如果 name 或 value 无效,则引发异常。请参阅 无效的名称和值。
来源
static VALUE
env_to_a(VALUE _)
{
VALUE ary = rb_ary_new();
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s) {
rb_ary_push(ary, rb_assoc_new(env_str_new(*env, s-*env),
env_str_new2(s+1)));
}
env++;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
return ary;
}
将 ENV 的内容作为包含 2 个元素的数组的 Array 返回,其中每个数组都是一个名称/值对
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_a # => [["bar", "1"], ["foo", "0"]]
来源
static VALUE
env_to_h(VALUE _)
{
VALUE hash = env_to_hash();
if (rb_block_given_p()) {
hash = rb_hash_to_h_block(hash);
}
return hash;
}
如果没有代码块,则返回一个 Hash,其中包含 ENV 中的所有名称/值对
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_h # => {"bar"=>"1", "foo"=>"0"}
如果提供了代码块,则返回一个 Hash,其项由代码块确定。将 ENV 中的每个名称/值对传递给代码块。代码块必须返回一个包含 2 个元素的 Array(名称/值对),该数组作为键和值添加到返回的 Hash
ENV.to_h { |name, value| [name.to_sym, value.to_i] } # => {:bar=>1, :foo=>0}
如果代码块未返回 Array,则抛出异常
ENV.to_h { |name, value| name } # Raises TypeError (wrong element type String (expected array))
如果代码块返回大小错误的 Array,则抛出异常
ENV.to_h { |name, value| [name] } # Raises ArgumentError (element has wrong array length (expected 2, was 1))
来源
static VALUE
env_f_to_hash(VALUE _)
{
return env_to_hash();
}
返回一个包含 ENV 中所有名称/值对的 Hash
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
来源
static VALUE
env_to_s(VALUE _)
{
return rb_usascii_str_new2("ENV");
}
返回 String ‘ENV’
ENV.to_s # => "ENV"
来源
static VALUE
env_update(int argc, VALUE *argv, VALUE env)
{
rb_foreach_func *func = rb_block_given_p() ?
env_update_block_i : env_update_i;
for (int i = 0; i < argc; ++i) {
VALUE hash = argv[i];
if (env == hash) continue;
hash = to_hash(hash);
rb_hash_foreach(hash, func, 0);
}
return env;
}
将给定 hash 中的每个键/值对添加到 ENV;返回 ENV
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('baz' => '2', 'bat' => '3') # => {"bar"=>"1", "bat"=>"3", "baz"=>"2", "foo"=>"0"}
删除值为 nil 的哈希对应的 ENV 条目
ENV.merge!('baz' => nil, 'bat' => nil) # => {"bar"=>"1", "foo"=>"0"}
对于已存在的名称,如果没有给出代码块,则覆盖 ENV 值
ENV.merge!('foo' => '4') # => {"bar"=>"1", "foo"=>"4"}
对于已存在的名称,如果给出了代码块,则将名称、其 ENV 值及其哈希值传递给代码块;代码块的返回值将成为新名称
ENV.merge!('foo' => '5') { |name, env_val, hash_val | env_val + hash_val } # => {"bar"=>"1", "foo"=>"45"}
如果名称或值无效,则抛出异常(请参阅 无效的名称和值);
ENV.replace('foo' => '0', 'bar' => '1') ENV.merge!('foo' => '6', :bar => '7', 'baz' => '9') # Raises TypeError (no implicit conversion of Symbol into String) ENV # => {"bar"=>"1", "foo"=>"6"} ENV.merge!('foo' => '7', 'bar' => 8, 'baz' => '9') # Raises TypeError (no implicit conversion of Integer into String) ENV # => {"bar"=>"1", "foo"=>"7"}
如果代码块返回无效名称,则抛出异常:(请参阅 无效的名称和值)
ENV.merge!('bat' => '8', 'foo' => '9') { |name, env_val, hash_val | 10 } # Raises TypeError (no implicit conversion of Integer into String) ENV # => {"bar"=>"1", "bat"=>"8", "foo"=>"7"}
请注意,对于上述异常,无效名称或值之前的哈希对会正常处理;之后的哈希对会被忽略。
来源
static VALUE
env_has_value(VALUE dmy, VALUE obj)
{
obj = rb_check_string_type(obj);
if (NIL_P(obj)) return Qnil;
VALUE ret = Qfalse;
ENV_LOCK();
{
char **env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s++) {
long len = strlen(s);
if (RSTRING_LEN(obj) == len && strncmp(s, RSTRING_PTR(obj), len) == 0) {
ret = Qtrue;
break;
}
}
env++;
}
FREE_ENVIRON(environ);
}
ENV_UNLOCK();
return ret;
}
如果 value 是某个环境变量名称的值,则返回 true,否则返回 false
ENV.replace('foo' => '0', 'bar' => '1') ENV.value?('0') # => true ENV.has_value?('0') # => true ENV.value?('2') # => false ENV.has_value?('2') # => false
来源
来源
static VALUE
env_values_at(int argc, VALUE *argv, VALUE _)
{
VALUE result;
long i;
result = rb_ary_new();
for (i=0; i<argc; i++) {
rb_ary_push(result, rb_f_getenv(Qnil, argv[i]));
}
return result;
}
返回一个 Array,其中包含与给定名称关联的环境变量值
ENV.replace('foo' => '0', 'bar' => '1', 'baz' => '2') ENV.values_at('foo', 'baz') # => ["0", "2"]
对于不是 ENV 名称的每个名称,Array 中返回 nil
ENV.values_at('foo', 'bat', 'bar', 'bam') # => ["0", nil, "1", nil]
如果没有给出任何名称,则返回一个空的 Array。
如果任何名称无效,则引发异常。请参阅 无效的名称和值。