本文簡要介紹ruby語言中 ENV[name] =
的用法。
用法
ENV[name] = value → value
store(name, value) → value
創建、更新或刪除指定的環境變量,並返回值。 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
無效,則引發異常。請參閱無效名稱和值。
相關用法
- Ruby ENV[name]用法及代碼示例
- Ruby ENV.empty?用法及代碼示例
- Ruby ENV.delete_if用法及代碼示例
- Ruby ENV.inspect用法及代碼示例
- Ruby ENV.delete用法及代碼示例
- Ruby ENV.to_s用法及代碼示例
- Ruby ENV.to_h用法及代碼示例
- Ruby ENV.clear用法及代碼示例
- Ruby ENV.length用法及代碼示例
- Ruby ENV.key用法及代碼示例
- Ruby ENV.reject用法及代碼示例
- Ruby ENV.values用法及代碼示例
- Ruby ENV.select!用法及代碼示例
- Ruby ENV.freeze用法及代碼示例
- Ruby ENV.rassoc用法及代碼示例
- Ruby ENV.invert用法及代碼示例
- Ruby ENV.value?用法及代碼示例
- Ruby ENV.select用法及代碼示例
- Ruby ENV.each_key用法及代碼示例
- Ruby ENV.reject!用法及代碼示例
- Ruby ENV.keys用法及代碼示例
- Ruby ENV.include?用法及代碼示例
- Ruby ENV.each_value用法及代碼示例
- Ruby ENV.except用法及代碼示例
- Ruby ENV類用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 ENV[name] =。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。