【发布时间】:2016-06-11 19:12:51
【问题描述】:
我有一个哈希数组,每个哈希只包含一个键/值对。有没有更有效的方法来访问密钥?这是我丑陋的解决方案
array_of_hashes = [
{:some => "stuff"},
{:other => "stuff"}
]
array_of_hashes.each do |hash|
hash.each do |key, value|
puts key
end
在我看来,一定有某种简单的说法
array_of_hashes.each do |hash|
puts hash.key # where this would simply access the key
end
或者可能
array_of_hashes.each do |hash|
puts hash.keys[0]
end
但这仍然感觉有点草率。
【问题讨论】:
-
array_of_hashes.each { |h| puts h.first.first } -
谢谢,我想这就是我的第二个选择: