【发布时间】:2017-08-07 19:39:18
【问题描述】:
我创建了套牌,洗牌,现在尝试将 7 张牌发到 4 位玩家手中。
得到以下错误:
irb(main):097:0* deck.each do
irb(main):098:1* Player.@hand << deck.pop()
irb(main):099:1> end
SyntaxError: (irb):98: syntax error, unexpected tIVAR, expecting '('
Player.@hand << deck.pop()
^
使用此代码:
class Player
attr_accessor :hand
def initialize(hand)
@hand = hand
end
end
【问题讨论】:
-
可以添加 Player 类吗?
-
您不应该使用
.@hand来访问类变量。见我上面的链接 -
@Royal 您需要某种形式的函数来操作类变量。大卫的回答就足够了。
标签: ruby loops variables methods block