如何从哈希中删除密钥?

时间:2012-11-19 03:24:36

标签: ruby hash-of-hashes

我有一堆散列代表一副牌的哈希,我试图弄清楚如何从牌组中删除一张特定的牌。请让我知道我应该学习或阅读的​​任何建议或方法。

@deck_of_cards = {
  :hearts => {:two => 2, :three => 3, :four => 4, :five => 5, :six => 6, :seven => 7, :eight => 8, :nine => 9, :ten => 10, :jack => 10, 
              :queen => 10, :king => 10, :ace => 11},
  :spades => {:two => 2, :three => 3, :four => 4, :five => 5, :six => 6, :seven => 7, :eight => 8, :nine => 9, :ten => 10, :jack => 10, 
              :queen => 10, :king => 10, :ace => 11},
  :clubs => {:two => 2, :three => 3, :four => 4, :five => 5, :six => 6, :seven => 7, :eight => 8, :nine => 9, :ten => 10, :jack => 10, 
              :queen => 10, :king => 10, :ace => 11},
  :diamonds => {:two => 2, :three => 3, :four => 4, :five => 5, :six => 6, :seven => 7, :eight => 8, :nine => 9, :ten => 10, :jack => 10, 
              :queen => 10, :king => 10, :ace => 11}
}

1 个答案:

答案 0 :(得分:4)

这很简单

def delete a, b
  @deck_of_cards[a].delete(b)
end

delete :hearts, :three

@deck_of_cards # => {:hearts=>{:two=>2, :four=>4, :five=>5, :six=>6, ...