0

如何在方法中传递嵌套哈希?这是一个示例代码

MONOPOLY_GAME = { 
  deeds: 
  { 
    boardwalk: 
    {  
      price: 400,
      rent: 50
    },
    atlantic: 
    { 
      price: 260, 
      rent: 22 
    },
    baltic:
    {
      price: 60, 
      rent: 4 
    }
  }
}
def rent_for(p)
  return MONOPOLY_GAME[:deeds][:p][:rent]
end

rent_for(:boardwalk)
rent_for(:atlantic)
rent_for(:baltic)
4

1 回答 1

5

您的方法似乎是正确的,除了您必须从 p 中删除冒号

def rent_for(p)
  MONOPOLY_GAME[:deeds][p][:rent]
end
于 2013-04-19T05:48:51.977 回答