如何在ruby中创建哈希哈希

时间:2012-12-09 19:45:06

标签: ruby

更新:我的答案在下面,简单的.merge是需要的。

我需要从build_virtual_boards_hash方法获取散列哈希作为输出 我无法弄清楚如何将哈希添加到哈希堆栈中。甚至如何创建这样的东西......首先我会列出方法,然后向你展示我得到的输出。

以下是使用的两种方法...问题位于'build_virtual_boards_hash'...

def build_virtual_boards_hash(board, player)
  virtual_board = board.dup

  virtual_board_hash = {}
  new_board_hash = {}

  empty_spaces_on_board = virtual_board.grid.select{ |k, v| v == " " }.keys
  index_mark = 'VB'+empty_spaces_on_board.length.to_s

  #THIS IS WHERE I THINK THE PROBLEM IS
  #HOW DO I ADD EACH LOOP RESULT TO -SOMETHING- 
  #THAT CAN BE CALLED ON LATER AFTER THE LOOP ENDS
  #SO I GET THE DESIRED RESULTING OUTPUT?
  # 
  empty_spaces_on_board.each do |empty_space_symbol|
    # create a hash
    new_board_hash = {index_mark => move_as_somebody(board, player, empty_space_symbol).grid} #value
    p new_board_hash  
  end

end


#takes board....returns new board
def move_as_somebody(board, player, empty_space)
  new_board = board
  if player == 'X'
    new_board.grid[empty_space] = player
  else
    player == 'O'
    new_board.grid[empty_space] = player
  end
  return new_board
end

我目前从这两种方法得到这个输出......

{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>" ", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>"X", :c3=>" "}}
{"VB7"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>"X", :c3=>"X"}}

我需要的是......

virtual_boards = {
    {"VB7"=>{:a1=>"X", :a2=>"X", :a3=>" ", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}},
    {"VB6"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>" ", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}},
    {"VB5"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>" ", :c1=>" ", :c2=>" ", :c3=>" "}},
    {"VB4"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>" ", :c2=>" ", :c3=>" "}},
    {"VB3"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>" ", :c3=>" "}},
    {"VB2"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>"X", :c3=>" "}},
    {"VB1"=>{:a1=>"X", :a2=>"X", :a3=>"X", :b1=>"X", :b2=>"O", :b3=>"X", :c1=>"X", :c2=>"X", :c3=>"X"}}
}

我如何做到这一点?

2 个答案:

答案 0 :(得分:4)

哈希哈希的问题通常是如何初始化深度哈希。

此问题已经解决并存入Ruby Facetes

h = Hash.autonew

现在您可以通过以下方式轻松初始化哈希:

h[:h1][:key] = 'value'
h[:h2][:key] = 'value'
...

答案 1 :(得分:1)

答案是....

#builds hash of hash of fake boards
def build_virtual_boards_hash(board, player)
  virtual_board = board.dup
  i = 0
  virtual_board_hash = {}
  new_board_hash = {}

  empty_spaces_on_board = virtual_board.grid.select{ |k, v| v == " " }.keys


  while i < empty_spaces_on_board.length do
    p empty_space_symbol = empty_spaces_on_board[i]
    p index_mark = 'VB'+i.to_s

    new_board_hash = {index_mark => move_as_somebody(board, player, empty_space_symbol).grid}

    virtual_board_hash = virtual_board_hash.merge(new_board_hash)        

    i += 1
  end
  p virtual_board_hash
end