如何用Yaml解析呢? [英] How can one parse this in Yaml?

查看:88
本文介绍了如何用Yaml解析呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个示例:

Example:
  number:
    cuarenta
    cuarenta y uno
    cuarenta y dos
    cuarenta y tres
    cuarenta y cuatro
    cuarenta y cinco
    cuarenta y seis
    cuarenta y siete
    cuarenta y ocho
    cuarenta y nueve

我想对此进行解析,以使每个数字都像"cuaranta y nueve"一样.我很难想象如何做到这一点,因为在YAML中,您变成了与名词"相关的字符串.

And I'd like to parse this so that I have each of those numbers together like "cuaranta y nueve". I'm having trouble visualizing how to do that since in YAML, you're turned a string associated with "noun".

我的Yaml解析器看起来像这样:

My yaml parser looks like this :

  File.open(Rails.root + 'lib/words/yamlicious.yml', 'r') do |file|
    YAML::load(file).each do |topic, word_types|
      temp_topic = Topic.create! name: topic
      temp_words = word_types.map{|type, words| words.split(' ').map {|word| Word.create type: type, word: word, topics: [temp_topic] } }
      temp_topic.words << temp_words
    end
  end

请注意,split部分会破坏这一点,因为那样的话,根据我的示例,我将得到一个单词,即三个单词"cuarenta","y"和"uno".

Notice the split part would ruin that because then I would get a word created per my example as three words "cuarenta", "y", and "uno".

推荐答案

要保留换行符,您必须使用

To preserve newlines, you have to use the pipe character:

Example:
  number: |
    cuarenta
    cuarenta y uno
    cuarenta y dos
    …

现在您可以split("\n")

这篇关于如何用Yaml解析呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆