大卫·布莱克的“基础扎实Rubyist”提供了一个例子来说明使用cycle
方法:最佳实践
class PlayingCard
SUITS = %w{ clubs diamonds hearts spades }
RANKS = %w{ 2 3 4 5 6 7 8 9 10 J Q K A }
class Deck
attr_reader :cards
def initialize(n=1)
@cards = []
SUITS.cycle(n) do |s|
RANKS.cycle(1) do |r|
@cards << "#{r} of #{s}"
end
end
end
end
end
deck = PlayingCard::Deck.new
我想访问变量@cards
的实例子中定义类。访问这个数组的最佳方法是什么?
我的理解是,我将不得不在Deck
中添加实例方法。有更好的技术吗?
指定手牌的最佳方式是什么?
从哪里访问? – sawa
什么是最好使用@卡数组来定义卡的随机手?这应该来自课堂甲板(或游戏卡)内部,还是最好在室外完成,然后参考课程?感谢您的帮助。 – Cameron