开发者

Getting an error in my Ruby script

开发者 https://www.devze.com 2023-02-12 01:16 出处:网络
class Card attr_accessor :number, :suit def initialize(number, suit) @number = number @suit = suit end def to_s
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消