【发布时间】:2015-07-18 03:42:17
【问题描述】:
我目前正在用 Ruby 开发一款扑克游戏。我没有使用大量 if-else 语句来检查玩家手牌的价值,而是决定执行以下操作:
#calculate the players score
def score
POSS.map {|check|
if (check[1].call())
@score = check[0]
puts @score
return check[0]
else
false
end
}
end
POSS = [
[10, :royal_flush?],
[9, :straight_flush?],
[8, :four_kind?],
[7, :full_house?],
[6, :flush?],
[5, :straight?],
[4, :three_kind?],
[3, :two_pairs?],
[2, :pair?]
]
“POSS”的每一项中的第二项是我创建的一种方法,用于检查玩家是否有那手牌。我正在尝试使用 .call() 调用该方法,但出现以下错误:
Player.rb:43:in `block in score': undefined method `call' for
:royal_flush?:Symbol (NoMethodError) from Player.rb:42:in `map' from
Player.rb:42:in `score' from Player.rb:102:in `get_score' from
Player.rb:242:in `<main>'
【问题讨论】:
标签: ruby-on-rails ruby methods