require 'iniparse'
conf = IniParse.parse(File.read('my.conf'))
如何在阅读之前检查conf['DEFAULT']
是否存在?
答案 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