我想知道如何解析以下内容的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
但它似乎不工作,并给我一个错误,值为nil。
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