How to access values from an array of hashes - Ranking poker hands in Ruby

59 Views Asked by At
@hands = [

  [{"suit": "heart", "value": 5},{"suit": "heart", "value": 8},{"suit": "spade", "value": 1},{"suit": "club", "value": 5},{"suit": "diamond", "value": 5}],
  [{"suit": "club", "value": 1},{"suit": "club", "value": 3},{"suit": "club", "value": 8},{"suit": "club", "value": 9},{"suit": "club", "value": 11}],
  [{"suit": "diamond", "value": 2},{"suit": "heart", "value": 2},{"suit": "spade", "value": 2},{"suit": "club", "value": 6},{"suit": "diamond", "value": 12}],
  [{"suit": "club", "value": 2},{"suit": "heart", "value": 3},{"suit": "spade", "value": 5},{"suit": "club", "value": 4},{"suit": "diamond", "value": 1}]
]

def get_hands
  @hands.each do |hand|
    hand.each do |card|
      print "#{card.fetch(:suit).chars[0]}" + "#{card.fetch(:value)}"
    end
  end
end

Output of get_hands is:

h5h8s1c5d5c1c3c8c9c11d2h2s2c6d12c2h3s5c4d1

Is there any way? that I can save the values from each hash hand so that I can push the 5 cards from each hand into their own variable. eg: Hand1, Hand2 etc?

I am a Ruby beginner and have been going round and round for few hours, now trying different things to get to even this point. I am just getting confused with my thought process from going round in circles.

0

There are 0 best solutions below