class Card
attr_accessor :number, :suit
def initialize(number, suit)
@number = number
@suit = suit
end
def to_s
"#{@number} of #{@suit}"
end
end
I'm assuming this creates a new array corre开发者_运维技巧ct? But why the use of the AT symbol? When should I use it and not use it?
@stack_of_cards = []
@stack << Card.new("A", "Spades")
puts @stack
# => BlackjackGame.rb:17: undefined method `<<' for nil:NilClass (NoMethodError)
Any ideas why this error is firing?
Exactly as it says in error: variable @stack
is not defined (or nil
).
Did you mean @stack_of_cards << ..
instead?
If you had warnings on (ruby -W2 script_name.rb
), you would have got a warning that @stack
is not merely nil, but undefined. See How do I debug Ruby scripts? for more hints on how to debug.
精彩评论