23

我正在尝试使用嵌套哈希。我有一副纸牌表示如下:

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}
}

我的目标是能够从牌组中取出一张特定的牌,并在没有那张牌的情况下归还牌组。任何人都可以帮助我如何遍历哈希并删除像两个俱乐部这样的卡吗?

deck_of_cards[:two][:clubs]

此代码可用于删除一副牌,但我不知道如何删除特定牌

deck_of_cards.delete_if {|k, v| k == :spades}
4

7 回答 7

40

只需这样做:

deck_of_cards[:clubs].delete(:two)
于 2012-06-20T18:02:46.107 回答
6

您也可以使用这样的点击函数删除元素并返回原始哈希

deck_of_cards.tap{|d| 
  d[:hearts].tap{|h| 
    h.delete(:two)
  }
}

这将返回没有 :two 键的 deck_if_cards 哈希

你也可以在一行中完成

    deck_of_cards.tap{|d| d[:hearts].tap{|h| h.delete("two")}}
于 2014-12-10T08:53:17.780 回答
2

采用.tap

deck_of_cards.tap{ |deck_of_cards| deck_of_cards[:hearts].delete(:two) }
#=> { 
#     :hearts=>{: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} 
#   }

具有以优雅的方式返回完整哈希而不是仅删除的值的好处。

于 2018-03-17T16:44:27.447 回答
0

你有一个哈希里面有一个哈希,所以你可以这样做:

deck_of_cards.each {|k,v| v.delete(:two) if k == :clubs}

您用于each遍历键和值,并在块内创建一个条件以删除内部哈希上的特定值。

于 2012-06-20T17:59:37.567 回答
0

你必须是这样的:

def remove_card deck, suit, number
  # do a deep clone
  new_deck = {}
  deck.each { |k, v| new_deck[k] = v.dup }

  # remove the card
  new_deck[suit] = new_deck[suit].reject { |k, v| k == number }

  new_deck
end

将你的牌组表示为一对数组可能会更好,如下所示:

[ [:hearts, :two], [:hearts, :three], ... ]

然后你可以去:

def remove_card deck, suit, number
  deck.reject { |(s, n)| n == number and s == suit }
end
于 2012-06-20T18:01:41.700 回答
0

我认为这种方法会为您完成工作

def nested_delete(card:, deck:)
  each do |hash_key, hash_value|
    if hash_key.to_s.eql?(deck)
      self[hash_key].delete(card.to_sym)
    end
  end
  self
end

假设你想从套牌“红心”中删除卡片“六”你需要做的就是

deck_of_cards.nested_delete(card: 'six', deck: 'hearts')
于 2018-11-19T12:30:02.273 回答
0

我确实在我的应用程序中将这个问题写为初始化程序,以添加一个调用delete_nested_keyHash 对象的方法。它删除哈希的嵌套键。您必须将 key_path 作为数组传递(只是要遍历的键列表以转到要删除的键)。

它似乎工作正常,但我只是写了它,所以它可能有问题。

class Hash
  module NestedKeyDeletion
    extend ActiveSupport::Concern

    included do
      def deleted_nested_key!(key_path)
        nested_hash = fetch_most_inner_hash(key_path)
        nested_hash.delete(key_path.last)

        self
      end

      private

      def fetch_most_inner_hash(key_path)
        nested_hash = self

        key_path.each_with_index do |key, index|
          return  nested_hash if index == key_path.size - 1
          nested_hash = nested_hash.fetch(key)
        end
      end
    end
  end
end

Hash.include(Hash::NestedKeyDeletion)

然后你可以像这样使用它:

[1] pry(main)> x = { x: { y: 2} }
=> {:x=>{:y=>2}}
[2] pry(main)> x.deleted_nested_key!([:x, :y])
=> {:x=>{}}
[3] pry(main)>

最好的问候,丹尼尔。

于 2021-06-22T14:23:23.223 回答