我有一个结构复杂的红宝石散列,如下所示:
something = {}
something[1488343493] = { :type => 'tag', :name => 'v1.2', :sha => 'a66fd116e454378794d24c41c193d385be37436f'}
something[1488288253] = { :type => 'pull', :number => '469', :sha => '190ed76e30a5fa7d357e8bfb78adfa687a673635', :title => "Consistent file uploads "}
something[1468674242] = { :type => 'tag', :name => 'v1.1', :sha => '2cf4549d0181ad1d60fbd3bbe132b599a14a8965'}
something[1488457772] = { :type => 'pull', :number => '476', :sha => '5f51fa23ea79bd9b89703cb93a5e38a0f0a338bb', :title => "Extract i18n strings in modals/* "}
something[1488288044] = { :type => 'pull', :number => '470', :sha => 'ab98ec3bf7cbe04f11a17d30ed07e5323b45d5df', :title => "Stop copy & clickthrough from list summaries "}
这基本上包含一个 Github 标签列表和合并的拉取请求。我可以很容易地对此进行排序.sort
:
something.sort.each do | key, value | # sorts perfectly fine
p "#{key} #{value[:type]} #{value[:sha]}"
end
但我不想要一个排序的哈希,而是一个反向的哈希。而且我完全不明白我根本无法扭转它,试图.reverse
让它给出NoMethodError
哈希:
something.reverse.each do | key, value | # undefined method `reverse' for #<Hash:0x0> (NoMethodError)
p "#{key} #{value[:type]} #{value[:sha]}"
end
尝试reverse_each
什么都不做:
something.reverse_each do | key, value | # does not reverse at all
p "#{key} #{value[:type]} #{value[:sha]}"
end
同样适用于转换为数组和反转,什么都不做:
gnihtemos = something.to_a.reverse.to_h # does not reverse at all
gnihtemos.each do | key, value |
p "#{key} #{value[:type]} #{value[:sha]}"
end
gnihtemos = Hash[something.to_a.reverse] # does not reverse at all
gnihtemos.each do | key, value |
p "#{key} #{value[:type]} #{value[:sha]}"
end
我的选择已经不多了。我正在使用 Ruby 2.4.0p0
。我还能做些什么来扭转something
?