2010-07-19 130 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