Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Ruby检查IniParse.parse中是否存在节_Ruby_Ruby On Rails 3 - Fatal编程技术网

如何使用Ruby检查IniParse.parse中是否存在节

如何使用Ruby检查IniParse.parse中是否存在节,ruby,ruby-on-rails-3,Ruby,Ruby On Rails 3,在阅读之前,如何检查是否存在conf['DEFAULT']?您可以用棍子戳它: require 'iniparse' conf = IniParse.parse(File.read('my.conf')) 或者你可以更聪明一点: if (conf.key?('DEFAULT')) # Congratulations, there's something there else # Uh-oh, it's missing. Panic? end 我这样做了:除非conf['agent'

在阅读之前,如何检查是否存在
conf['DEFAULT']

您可以用棍子戳它:

require 'iniparse'

conf = IniParse.parse(File.read('my.conf'))
或者你可以更聪明一点:

if (conf.key?('DEFAULT'))
  # Congratulations, there's something there
else
  # Uh-oh, it's missing. Panic?
end

我这样做了:除非conf['agent'].nil?在Ruby中,逻辑上唯一错误的是
nil
false
,所以除非你期望
conf['agent']
是字面上的
false
,否则你可以做
if-conf['agent']
,并确信它会工作。由于您可能正在查找字符串、哈希结构或数组,
false
无论如何都不是您感兴趣的东西。
case (conf['DEFAULT'])
when Hash
  # Great, that's what I was expecting!
when nil
  # Uh-oh, it's missing. Panic?
else
  # That's not what I was expecting.
end