我想知道如何解析一个具有以下内容的YAML文件:
---
javascripts:
- fo_global:
- lazyload-min
- holla-min
目前我正在尝试以这种方式解析:
@custom_asset_packages_yml = (File.exists?("#{RAILS_ROOT}/config/asset_packages.yml") ? YAML.load_file("#{RAILS_ROOT}/config/asset_packages.yml") : nil)
if !@custom_asset_packages_yml.nil?
@custom_asset_packages_yml['javascripts'].each{ |js|
js['fo_global'].each{ |script|
script
}
}
end
但似乎它不起作用,并给我一个值为空的错误。
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
如果我尝试这样做,它就会输出整个字符串(fo_globallazyload-minholla-min):
if !@custom_asset_packages_yml.nil?
@custom_asset_packages_yml['javascripts'].each{ |js|
js['fo_global']
}
end