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。
这里有什么
首先,其他部分。Class ENV
-
继承自 Object 类。
-
扩展了 module 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 元素数组。 -
::clone:引发异常。 -
::fetch:返回给定名称的值。 -
::keys:返回所有名称的数组。 -
::rassoc:返回具有给定值的第一个条目的名称和值。 -
::reject:返回由块未拒绝的条目组成的哈希。 -
::slice:返回给定名称及其对应值的哈希。 -
::to_a:将条目作为 2 元素数组的数组返回。 -
::to_h:返回由块选择的条目组成的哈希。 -
::to_hash:返回所有条目组成的哈希。 -
::to_s:返回字符串'ENV'。 -
::values:返回所有值的数组。 -
::values_at:返回给定名称的值的数组。
更多方法
Public Class Methods
Source
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 无效,则引发异常。请参阅 无效的名称和值。
Source
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 无效,则引发异常。请参阅 无效的名称和值。
Source
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;
}
}
如果名为 name 的环境变量存在,则返回一个包含该环境变量的名称和值的 2 元素 Array。
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)
Source
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
Source
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");
}
Source
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;
}
删除命名的环境变量(如果存在)并返回其值。
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 无效,则引发异常。请参阅 无效的名称和值。
Source
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
Source
static VALUE
env_dup(VALUE obj)
{
rb_raise(rb_eTypeError, "Cannot dup ENV, use ENV.to_h to get a copy of ENV as a hash");
}
Source
static VALUE
env_each_pair(VALUE ehash)
{
long i;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
VALUE ary = rb_ary_new();
rb_encoding *enc = env_encoding();
ENV_LOCKING() {
char **env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s) {
rb_ary_push(ary, env_str_new(*env, s-*env, enc));
rb_ary_push(ary, env_str_new2(s+1, enc));
}
env++;
}
FREE_ENVIRON(environ);
}
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"}
Source
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"]
Source
static VALUE
env_each_pair(VALUE ehash)
{
long i;
RETURN_SIZED_ENUMERATOR(ehash, 0, 0, rb_env_size);
VALUE ary = rb_ary_new();
rb_encoding *enc = env_encoding();
ENV_LOCKING() {
char **env = GET_ENVIRON(environ);
while (*env) {
char *s = strchr(*env, '=');
if (s) {
rb_ary_push(ary, env_str_new(*env, s-*env, enc));
rb_ary_push(ary, env_str_new2(s+1, enc));
}
env++;
}
FREE_ENVIRON(environ);
}
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"}
Source
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"]
Source
static VALUE
env_empty_p(VALUE _)
{
bool empty = true;
ENV_LOCKING() {
char **env = GET_ENVIRON(environ);
if (env[0] != 0) {
empty = false;
}
FREE_ENVIRON(environ);
}
return RBOOL(empty);
}
当没有环境变量时返回 true,否则返回 false。
ENV.clear ENV.empty? # => true ENV['foo'] = '0' ENV.empty? # => false
Source
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"}
Source
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 无效,则引发异常。请参阅 无效的名称和值。
Source
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"}
Source
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
Source
static VALUE
env_freeze(VALUE self)
{
rb_raise(rb_eTypeError, "cannot freeze ENV");
UNREACHABLE_RETURN(self);
}
引发异常。
ENV.freeze # Raises TypeError (cannot freeze ENV)
Source
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)
Source
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_LOCKING() {
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);
}
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
Source
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)
Source
static VALUE
env_inspect(VALUE _)
{
VALUE str = rb_str_buf_new2("{");
rb_encoding *enc = env_encoding();
ENV_LOCKING() {
char **env = GET_ENVIRON(environ);
while (*env) {
const char *s = strchr(*env, '=');
if (env != environ) {
rb_str_buf_cat2(str, ", ");
}
if (s) {
rb_str_buf_append(str, rb_str_inspect(env_enc_str_new(*env, s-*env, enc)));
rb_str_buf_cat2(str, " => ");
s++;
rb_str_buf_append(str, rb_str_inspect(env_enc_str_new(s, strlen(s), enc)));
}
env++;
}
FREE_ENVIRON(environ);
}
rb_str_buf_cat2(str, "}");
return str;
}
将环境的内容作为字符串返回。
ENV.replace('foo' => '0', 'bar' => '1') ENV.inspect # => "{\"bar\"=>\"1\", \"foo\"=>\"0\"}"
Source
static VALUE
env_invert(VALUE _)
{
return rb_hash_invert(env_to_hash());
}
Source
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"}
Source
static VALUE
env_key(VALUE dmy, VALUE value)
{
StringValue(value);
VALUE str = Qnil;
rb_encoding *enc = env_encoding();
ENV_LOCKING() {
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, enc);
break;
}
}
env++;
}
FREE_ENVIRON(environ);
}
return str;
}
Source
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)
Source
static VALUE
env_f_keys(VALUE _)
{
return env_keys(FALSE);
}
Source
static VALUE
env_size(VALUE _)
{
return INT2FIX(env_size_with_lock());
}
返回环境变量的数量。
ENV.replace('foo' => '0', 'bar' => '1') ENV.length # => 2 ENV.size # => 2
Source
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)
Source
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"}
请注意,对于上述异常,在无效名称或值之前的哈希对正常处理;之后的哈希对被忽略。
Source
static VALUE
env_rassoc(VALUE dmy, VALUE obj)
{
obj = rb_check_string_type(obj);
if (NIL_P(obj)) return Qnil;
VALUE result = Qnil;
ENV_LOCKING() {
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);
}
return result;
}
Source
static VALUE
env_none(VALUE _)
{
return Qnil;
}
Source
static VALUE
env_reject(VALUE _)
{
return rb_hash_delete_if(env_to_hash());
}
将每个环境变量名称及其值作为 2 元素 Array 传递给块。返回一个由块决定的哈希。当块返回真值时,将该键值对添加到返回的 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"}
Source
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
Source
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"}
Source
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"}
Source
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
Source
static VALUE
env_shift(VALUE _)
{
VALUE result = Qnil;
VALUE key = Qnil;
rb_encoding *enc = env_encoding();
ENV_LOCKING() {
char **env = GET_ENVIRON(environ);
if (*env) {
const char *p = *env;
char *s = strchr(p, '=');
if (s) {
key = env_str_new(p, s-p, enc);
VALUE val = env_str_new2(getenv(RSTRING_PTR(key)), enc);
result = rb_assoc_new(key, val);
}
}
FREE_ENVIRON(environ);
}
if (!NIL_P(key)) {
env_delete(key);
}
return result;
}
Source
static VALUE
env_size(VALUE _)
{
return INT2FIX(env_size_with_lock());
}
返回环境变量的数量。
ENV.replace('foo' => '0', 'bar' => '1') ENV.length # => 2 ENV.size # => 2
Source
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)
Source
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 无效,则引发异常。请参阅 无效的名称和值。
Source
static VALUE
env_to_a(VALUE _)
{
VALUE ary = rb_ary_new();
rb_encoding *enc = env_encoding();
ENV_LOCKING() {
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, enc),
env_str_new2(s+1, enc)));
}
env++;
}
FREE_ENVIRON(environ);
}
return ary;
}
将 ENV 的内容作为 2 元素数组的数组返回,每个数组都是一个键值对。
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_a # => [["bar", "1"], ["foo", "0"]]
Source
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;
}
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_h # => {"bar"=>"1", "foo"=>"0"}
带块时,返回一个由块决定的哈希。 ENV 中的每个键值对都将传递给块。块必须返回一个 2 元素 Array(键值对),该对将作为键和值添加到返回的 Hash 中。
ENV.to_h { |name, value| [name.to_sym, value.to_i] } # => {bar: 1, foo: 0}
如果块未返回数组,则引发异常。
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))
Source
static VALUE
env_f_to_hash(VALUE _)
{
return env_to_hash();
}
Source
static VALUE
env_to_s(VALUE _)
{
return rb_usascii_str_new2("ENV");
}
返回字符串 'ENV'。
ENV.to_s # => "ENV"
Source
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"}
请注意,对于上述异常,在无效名称或值之前的哈希对正常处理;之后的哈希对被忽略。
Source
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_LOCKING() {
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);
}
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
Source
static VALUE
env_f_values(VALUE _)
{
return env_values();
}
Source
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;
}