Ruby-on-rails 如何读取 YAML 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13310488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I read a YAML file?
提问by newbi
I have such a YAML file:
我有这样一个 YAML 文件:
Company1:
name: Something1
established: 2000
#
Company2:
name: Something2
established: 1932
reading the YAML file: (** UPDATE **)
读取 YAML 文件:(** 更新 **)
config = YAML.load_file('file.yaml')
config.each do |key, value|
if(key == 'name')
company_name = value
#year = config['Company1']['established']
year = config.fetch(key)['established']
end
end
** UPDATE ** Now the above code is working, but it shows the result as:
** UPDATE ** 现在上面的代码正在运行,但它显示的结果为:
company1 => {"name" => "something1"} => {"established year" => 2000"}
how can I remove the the {} and "" ?
如何删除 {} 和 "" ?
回答by Steve Robinson
Okay, so this is your YAML file right?
好的,这是您的 YAML 文件,对吗?
Company1:
name: Something1
established: 2000
Company2:
name: Something2
established: 1932
Okay now this YAML file actually represents a Hash. The has has two keys i.e Company1, Company2 (because they are the leading entries and the sub entries (name and established) are indented under them). The value of these two keys is again a Hash. This Hash also has 2 keys namely name and established. And they have values like Something1 and 2000 respectively etc.
好的,现在这个 YAML 文件实际上代表了一个哈希。有两个键,即 Company1、Company2(因为它们是前导条目,并且子条目(名称和已建立)在它们下面缩进)。这两个键的值又是一个哈希值。这个 Hash 也有 2 个键,即 name 和 created。它们分别具有Something1和2000等值。
So when you do,
所以当你这样做时,
config=YAML.load_file('file.yml')
And print config (which is a Hash representing the YAML file contents) using,
并使用以下方法打印配置(它是表示 YAML 文件内容的哈希),
puts config
you get following output:
你得到以下输出:
{"Company1"=>{"name"=>"Something1", "established"=>2000}, "Company2"=>{"name"=>"Something2", "established"=>1932}}
So we have a Hash object as described by the YAML file.
所以我们有一个由 YAML 文件描述的 Hash 对象。
Using this Hash is pretty straight forward.
使用这个哈希非常简单。
Since each company's name and year come in a separate hash held by the outer hash (company1, company2), we can iterate through the companies. The following Code prints the Hash.
由于每个公司的名称和年份都在由外部哈希(company1、company2)保存的单独哈希中,我们可以遍历公司。以下代码打印哈希。
config.each do |company,details|
puts company
puts "-------"
puts "Name: " + details["name"]
puts "Established: " + details["established"].to_s
puts "\n\n"
end
So in Each iteration we get access to each (key,value) of the Hash. This in first iteration we have company(key) as Company1and details(value) as {"name"=>"Something1", "established"=>2000}
所以在每次迭代中,我们可以访问哈希的每个(键,值)。这在第一次迭代中我们有company(key) asCompany1和details(value) as{"name"=>"Something1", "established"=>2000}
Hope this helped.
希望这有帮助。
回答by Mori
YAML uses indentation for scoping, so try, e.g.:
YAML 使用缩进进行范围界定,因此请尝试,例如:
Company1:
name: Something1
established: 2000
Company2:
name: Something2
established: 1932

