-2
說我有這樣的哈希:如何在嵌套散列中搜索特定鍵的值?
[82] pry(main)> commit2
=> {:sha=>"4df2b779ddfcb27761c71e00e2b241bfa06a0950",
:commit=>
{:author=>
{:name=>"asasa asasa",
:email=>"[email protected]",
:date=>2016-08-06 16:24:04 UTC,
:sha=> "876239789879ab9876c8769287698769876fed"},
:committer=>
{:name=>"asasa asasa",
:email=>"[email protected]",
:date=>2016-08-06 16:26:45 UTC},
:message=>
"applies new string literal convention in activerecord/lib\n\nThe current code base is not uniform. After some discussion,\nwe have chosen to go with double quotes by default.",
:tree=>
{:sha=>"7a83cce62195f7b20afea6d6a8873b953d25cb84",
:url=>
"https://api.github.com/repos/rails/rails/git/trees/7a83cce62195f7b20afea6d6a8873b953d25cb84"},
:url=>
"https://api.github.com/repos/rails/rails/git/commits/4df2b779ddfcb27761c71e00e2b241bfa06a0950",
:comment_count=>0},
:url=>
"https://api.github.com/repos/rails/rails/commits/4df2b779ddfcb27761c71e00e2b241bfa06a0950",
:html_url=>
"https://github.com/rails/rails/commit/4df2b779ddfcb27761c71e00e2b241bfa06a0950",
:comments_url=>
"https://api.github.com/repos/rails/rails/commits/4df2b779ddfcb27761c71e00e2b241bfa06a0950/comments"
}
}
}
該散列有很多嵌套的哈希值,但我要檢查,看看是否有任何嵌套的散列有876239789879ab9876c8769287698769876fed
一個:sha
值。
在上面的例子中,它應該返回[:commit][:author]
散列,因爲那個有:sha
的鍵值與我們正在尋找的鍵相同。
我該怎麼做?
那你試試? –
此外,請使用正確的語法發佈完整的Ruby對象。 –
@EricDuminil我修改了它的代碼,使它成爲一個完整的Ruby對象,語法正確。 – marcamillion