Ruby 私有实例变量,但有例外
我正在用红宝石制作纸牌游戏。
我有 Game 类,它有一个 Player 对象数组。
array_of_players = Array[
Player.new("Ben"),
Player.new("Adam"),
Player.new("Peter"),
Player.new("Fred"),
]
my_game = Game.new(array_of_players)
puts my_game.players[2].name #=> Peter
每个玩家也可以访问游戏,这样他们就可以访问游戏的重要部分,就像
self.game.last_card_dealt
每个玩家也有卡牌(Player.cards)一样,我想确保玩家不能访问彼此的卡牌。但是,游戏确实需要访问卡牌,所以我认为使用 private
不合适,而且玩家需要访问彼此的一些信息,所以我不认为我想要这样要么是私有
...
基本上,我希望这些能够工作。
self.cards #where self is a Player object
self.players[0].cards #where self is the Game
self.game.players[0].name #where self is a Player object
这会失败:
self.hand.players[0].cards #=> Nice try sucker! Cheating is for losers.
如何处理像这样的更复杂的权限? 谢谢。
I am making a card game in ruby.
I have the Game class, which has an array of Player objects.
array_of_players = Array[
Player.new("Ben"),
Player.new("Adam"),
Player.new("Peter"),
Player.new("Fred"),
]
my_game = Game.new(array_of_players)
puts my_game.players[2].name #=> Peter
Each player also has access to the Game, so that they can access the important bits of the game like so
self.game.last_card_dealt
Each player also has cards (Player.cards), and I want to make sure that players can't access each other's cards. However, the Game does need access to the cards, so I don't think using private
is appropriate, and the players need access to some of each other's information, so I don't think I want that to be private
either...
Basically, I want these to work.
self.cards #where self is a Player object
self.players[0].cards #where self is the Game
self.game.players[0].name #where self is a Player object
And this to fail:
self.hand.players[0].cards #=> Nice try sucker! Cheating is for losers.
How are more complex permissions like this handled?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这比我的其他答案更实用,并使用 Game 对象作为游戏本身所有信息(玩家、卡牌等)的委托。请注意,您仍然必须相信呼叫者能够自行传递,但说真的,您在哪里划清界限呢?
输出:
This is more practical than my other answer, and uses the Game object as a delegate to all information in the game itself (Players, Cards, etc.). Note that you still have to trust the caller to pass themselves, but seriously where do you draw the line?
The output:
将 Game.player 保持私有以禁止玩家通过数组访问其他玩家。
例如,当
self
是玩家时,self.game.players[0].name
有点愚蠢。也许您想要一个仅返回玩家姓名数组的公共
Game.player_names
方法?最重要的是,您可以创建一个公共
Players.opponents
方法。示例
Game.player_names
Player.opponents
Keep
Game.player
private to disallow players from accessing other players through the array.e.g., When
self
is a player,self.game.players[0].name
is kind of silly.Perhaps you'd like a public
Game.player_names
method that just returns an array of player names?On top of that, you could make a public
Players.opponents
method.Examples
Game.player_names
Player.opponents
这玩起来很有趣。我不确定这是否是最好的答案,但它确实有效。关键是将调用对象传递给 Player.cards(obj),并检查它是否是 Player 本身或 Game 类型,两者都具有合法访问权限。
和输出:
This was fun to play with. I'm not sure if this is the best possible answer, but it works. The key is to pass the calling object to Player.cards(obj), and check if it's either the Player itself, or of type Game, both of which have legal access.
And the output:
感谢您的所有回复。
最后我想我可以给授权对象一个密钥,用于允许它访问方法的核心。
游戏对象具有@auth_object并将其设置为要访问其秘密方法的玩家对象,并且玩家秘密方法检查hand.auth_object是否为
self
,否则不执行任何操作。然后@auth_object被设置回nil。 @auth_object 有一个 attr_reader 但没有 writer。那行得通。
Thanks for all your responses.
In the end I figured that I could give the authorised object a key which is used to allow it access to the meat of a method.
Game object has @auth_object and sets it to the player object it intends to access the secret methods of, and the player secret method checks if hand.auth_object is
self
, otherwise it does nothing. Then @auth_object is set back to nil. There is an attr_reader but no writer for @auth_object.That works.