如何将YAML文件中的字段解析为同一文件中的另外三个字段?

时间:2013-07-22 23:01:33

标签: ruby-on-rails ruby yaml to-yaml

我有一个包含以下内容的YAML文件:

cat:
  name: Cat
  description: catlike reflexes
dog:
  name: Dog
  description: doggy breath

我想解析它并将描述分成key1key2,如下所示:

cat:
  name: Cat
  description: catlike reflexes
  info:
    key1: catlike
    key2: reflexes
dog:
  name: Dog
  description: doggy breath
  info:
    key1: doggy
    key2: breath

但是,出于某种原因,我无法正确地做到这一点。我到目前为止所尝试的是以下代码的变体,我认为我过于复杂:

# to get the original file's data
some_data = YAML.load(File.open("#{Rails.root}/config/some_data.yml"))

new_data = some_data.collect do |old_animal|
  animal = old_animal.second

  if animal && animal["description"]
    new_blocks = Hash.new
    blocks = animal["description"].split(" ")
    new_blocks["key1"] = blocks.first
    new_blocks["key2"] = blocks.second
    animal["info"] = new_blocks
  end
  old_animal.second = animal
  old_animal
end

# to write over the original file
File.write("#{Rails.root}/config/some_data.yml", new_data.to_yaml)

1 个答案:

答案 0 :(得分:1)

你没有说你是否可以在描述中有多个单词,但这有点常识,所以我会做这样的事情:

require 'yaml'

data = YAML.load(<<EOT)
cat:
  name: Cat
  description: catlike reflexes rules
dog:
  name: Dog
  description: doggy breath
EOT
data # => {"cat"=>{"name"=>"Cat", "description"=>"catlike reflexes rules"}, "dog"=>{"name"=>"Dog", "description"=>"doggy breath"}}

此时,来自YAML文件的数据被加载到哈希中。迭代每个散列键/值对:

data.each do |(k, v)|
  descriptions = v['description'].split
  keys = descriptions.each_with_object([]) { |o, m| m << "key#{(m.size + 1)}" }
  hash = keys.each_with_object({}) { |o, m| m[o] = descriptions.shift }
  data[k]['info'] = hash
end

这是我们回来的:

data # => {"cat"=>{"name"=>"Cat", "description"=>"catlike reflexes rules", "info"=>{"key1"=>"catlike", "key2"=>"reflexes", "key3"=>"rules"}}, "dog"=>{"name"=>"Dog", "description"=>"doggy breath", "info"=>{"key1"=>"doggy", "key2"=>"breath"}}}

如果输出它会是什么样子:

puts data.to_yaml
# >> ---
# >> cat:
# >>   name: Cat
# >>   description: catlike reflexes rules
# >>   info:
# >>     key1: catlike
# >>     key2: reflexes
# >>     key3: rules
# >> dog:
# >>   name: Dog
# >>   description: doggy breath
# >>   info:
# >>     key1: doggy
# >>     key2: breath

each_with_objectinject类似,但使用起来更清晰,因为它不需要我们返回我们正在积累的对象。