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

时间:2017-04-27 22:01:28

标签: ruby ruby-on-rails-3

require 'iniparse'

conf = IniParse.parse(File.read('my.conf'))

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

1 个答案:

答案 0 :(得分:1)

你可以用棍子捅它:

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

或者你可以更聪明一点:

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