这是什么样的数据结构?

时间:2010-07-19 22:28:49

标签: ruby data-structures

我从github提取最近的提交并尝试使用ruby解析它。我知道我可以手动解析它,但我想看看是否有一些软件包可以将其转换为哈希或其他数据结构。

commits: 
- parents: 
  - id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
  author:
    name: This guy
    login: tguy
    email: tguy@tguy.com
  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: oguy@gmail.com
  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

4 个答案:

答案 0 :(得分:5)

这是YAML http://ruby-doc.org/core/classes/YAML.html。您可以执行类似obj = YAML::load yaml_string(文件顶部的require 'yaml',标准库中的{{1}}),然后像嵌套哈希一样访问它。

YAML基本上用于ruby世界,就像人们在java / c#worlds中使用XML一样。

答案 1 :(得分:4)

对我来说就像是YAML。有许多语言的解析器。例如,随Ruby包含的YAML库:

data = <<HERE
commits: 
- parents: 
  - id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
  author:
    name: This guy
    login: tguy
    email: tguy@tguy.com
  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: oguy@gmail.com
  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"=>"tguy@tguy.com"},
    "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"=>"oguy@gmail.com"},
    "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"}]}

答案 2 :(得分:2)

此格式为YAML,但您可以使用XML或JSON获取相同的信息,请参阅General API Information。我确信有些库可以在Ruby中解析这些格式。

答案 3 :(得分:0)

虽然这不是您正在寻找的内容,但这里有一些关于提交提交的更多信息。 http://develop.github.com/p/commits.html。否则,我认为您可能只需要手动解析它。