2012-08-14 42 views
0

假设我有这样的例子:如何解析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Ÿnueve”。在YAML中,我很难想象如何做到这一点,因此您将一个与“名词”相关联的字符串变成了一个字符串。

我YAML解析器是这样的:

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 ”。

回答

4

要保留换行,你必须使用pipe character

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

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

+0

啊天啊我从来没有见过这个!谢谢Stefan! – Trip 2012-08-17 09:07:14

+1

不客气:-) – Stefan 2012-08-17 09:08:49