This is a rock paper scissors game. From irb, game.class says it's an array. I hope to find the name of the person who won the game (in this case Player2).
game = [["Player1", "P"], ["Player2", "S"]]
The approach that comes to mind is to return a Hash with the name values split up. Then search that hash via the value to get the player name.
h = Hash.new(0)
game.collect do |f|
h[f] = f[1]
end
h
#=> {["Player1", "P"]=>"P", ["Player2", "S"]=>"S"}
This is close but no cigar. I want
{"Player1" => "P", "Player2" => "S"}
I tried again with inject method:
game.flatten.inject({}) do |player, tactic|
player[tactic] = tactic
player
end
#=> {"Player1"=>"Player1", "P"=>"P", "Player2"=>"Player2", "S"=>"S"}
This did not work:
Hash[game.map {|i| [i(0), i(1)] }]
#=> NoMethodError: undefined method `i' for main:Object
I would appreciate some pointers to something that will help me understand.