【发布时间】:2017-02-13 14:05:48
【问题描述】:
我正在学习 The Well Grounded Rubyist,但我无法理解如何访问存储在子类 Deck 的实例 var @cards 中的数组。
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
two_decks = PlayingCard::Deck.new(2)
puts two_decks
# => #<PlayingCard::Deck:0x007fb5c2961e80>
这是有道理的,它从 PlayingCard::Deck 返回 two_decks 的对象 id。为了使它更有用,我想出的访问存储在@cards 中的数组的唯一方法是添加另一个方法 Deck#show。现在我可以在@cards 上调用其他方法,就像我开始做的那样。这个简单的例子允许获取@cards的计数:
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
def show
@cards
end
end
end
two_decks = PlayingCard::Deck.new(2).show
p two_decks.count
# => 104
我很困惑,因为我认为 attr_reader 允许在课堂外看到 @cards 实例变量。 Cards#show 方法是否增加了变量的范围?我错过了更好的方法吗?我是否应该从@cards 收集操作/信息?谢谢!
【问题讨论】:
-
@cards是一个实例变量,attr_reader所做的是允许您访问(读取其值)此实例变量,您已在show方法中成功完成。 -
你还在迷茫吗?你明白安德烈的评论吗?
-
非常感谢。起初我以为它可以在 PlayingCard 类之外使用。
-
我是否有更合适的方式来访问@cards 表示的数组?除了在 PlayingCard::Deck.new 实例上创建/调用 show 方法之外?
标签: ruby