2010-07-19 173 views
2

我拉從github最近的提交,並嘗試使用紅寶石解析它。我知道我可以手動解析它,但我想看看是否有一些軟件包可以將它變成散列或其他數據結構。這是什麼樣的數據結構?

commits: 
- parents: 
    - id: 202fb79e8686ee127fe49497c979cfc9c9d985d5 
    author: 
    name: This guy 
    login: tguy 
    email: [email protected] 
    url: a url 
    id: e466354edb31f243899051e2119f4ce72bafd5f3 
    committed_date: "2010-07-19T13:44:43-07:00" 
    authored_date: "2010-07-19T13:33:26-07:00" 
    message: |- 
    message 
- parents: 
    - id: c3c349ec3e9a3990cac4d256c308b18fd35d9606 
    author: 
    name: Other Guy 
    login: oguy 
    email: [email protected] 
    url: another url 
    id: 202fb79e8686ee127fe49497c979cfc9c9d985d5 
    committed_date: "2010-07-19T13:44:11-07:00" 
    authored_date: "2010-07-19T13:44:11-07:00" 
    message: this is another message 

回答

5

這是YAML http://ruby-doc.org/core/classes/YAML.html。你可以做一些像obj = YAML::load yaml_string(和一個require 'yaml'在你的文件的頂部,它的標準庫),然後像嵌套散列一樣訪問它。

YAML基本上是用在人們在java/c#世界中使用XML的方式在ruby世界中使用的。

2

此格式爲YAML,但您可以使用XML或JSON獲取相同的信息,請參閱General API Information。我相信有一些庫可以在Ruby中解析這些格式。

+0

引入nokogiri解析XML:http://nokogiri.org/ – 2010-07-19 22:36:25

4

看起來像YAML給我。有許多語言的解析器。例如,與YAML庫附帶紅寶石:

data = <<HERE 
commits: 
- parents: 
    - id: 202fb79e8686ee127fe49497c979cfc9c9d985d5 
    author: 
    name: This guy 
    login: tguy 
    email: [email protected] 
    url: a url 
    id: e466354edb31f243899051e2119f4ce72bafd5f3 
    committed_date: "2010-07-19T13:44:43-07:00" 
    authored_date: "2010-07-19T13:33:26-07:00" 
    message: |- 
    message 
- parents: 
    - id: c3c349ec3e9a3990cac4d256c308b18fd35d9606 
    author: 
    name: Other Guy 
    login: oguy 
    email: [email protected] 
    url: another url 
    id: 202fb79e8686ee127fe49497c979cfc9c9d985d5 
    committed_date: "2010-07-19T13:44:11-07:00" 
    authored_date: "2010-07-19T13:44:11-07:00" 
    message: this is another message 
HERE 

pp YAML.load data 

它打印:

{"commits"=> 
    [{"author"=>{"name"=>"This guy", "login"=>"tguy", "email"=>"[email protected]"}, 
    "parents"=>[{"id"=>"202fb79e8686ee127fe49497c979cfc9c9d985d5"}], 
    "url"=>"a url", 
    "id"=>"e466354edb31f243899051e2119f4ce72bafd5f3", 
    "committed_date"=>"2010-07-19T13:44:43-07:00", 
    "authored_date"=>"2010-07-19T13:33:26-07:00", 
    "message"=>"message"}, 
    {"author"=> 
    {"name"=>"Other Guy", "login"=>"oguy", "email"=>"[email protected]"}, 
    "parents"=>[{"id"=>"c3c349ec3e9a3990cac4d256c308b18fd35d9606"}], 
    "url"=>"another url", 
    "id"=>"202fb79e8686ee127fe49497c979cfc9c9d985d5", 
    "committed_date"=>"2010-07-19T13:44:11-07:00", 
    "authored_date"=>"2010-07-19T13:44:11-07:00", 
    "message"=>"this is another message"}]} 
+0

這是偉大的,我只是現在的問題是如何我會打印「這個傢伙」如果我已經分配了更多的文件= YAML.load數據,我將如何訪問 – Tom 2010-07-20 00:20:44

+0

@Tom:你只需要經過樹到你想要的值:'morestuff ['commitits'] [ 0] ['author'] ['name']'(對於「其他人」,它將是'morestuff ['commitits'] [1] ['author'] ['name']') – Chuck 2010-07-20 00:44:09