2011-05-18 59 views
2

我是新來YAML,並試圖做一個YAML塊,但我收到以下錯誤YAML塊:錯誤而解析文本

while scanning a block mapping: expected , but found: #< ScalarToken Value="content: | This is a test of a test test test test A very very good test yeah yeah A test of test test" Style="None"> (line 8, column 1)

雖然作品,未經精|,我想空白保存。

YAML文件(Home.yml):

--- 
section: 
    title: About this site 
    content: | 
     This is a test of a test test test test 
     A very very good test 
     A test of test test 
section: 
    title: Source code 
    content: | 
     Licens:: BSD 
     Link:: Here 
     Foo 
... 

Ruby代碼:

home = YAML.load_file('data/Home.yml') 
home.inspect 

回答

1

哪些YAML解析器您使用的? Ruby 1.8.7解析器和1.9中的解析器在你的問題中解析YAML。

還有問題,通過。你給的語法是這樣的哈希:

{ 
    'section' => { 
    'title' => "About this site", 
    'content => ... 
    } 
    'section' => { 
    'title' => 'Source code', 
    'content' => ... 
    } 
} 

但是,你不能有兩個哈希鍵是相同的。發生的是最後一個勝利。您可能正在尋找一組哈希。要做到這一點,利用這個YAML語法:

--- 
- 
    section: 
    title: About this site 
    content: | 
     This is a test of a test test test test 
     A very very good test 
     A test of test test 
- 
    section: 
    title: Source code 
    content: | 
     Licens:: BSD 
     Link:: Here 
     Foo 
+1

我發現YAML禁止標籤,因爲它是一種空白的它是不可能的,我通過看實例知道。我確實將第二件事糾正爲列表,因爲這只是我忘了的一件事,但無論如何感謝您的幫助! – 2011-05-18 19:31:33

+0

我使用的是默認值?其中一個IronRuby 1.1.2應該與Ruby 1.9兼容 – 2011-05-18 19:32:10

+0

@Yet另一個極客:哦,是的,標籤:繼續奉獻的禮物。我很高興你把它理順了。 – 2011-05-18 19:36:29