在我的代码中尝试p @stack
在课堂外打印时,nil
尽管放置了attr_accessor :stacks, :each_stack_size, :stack_number
. 像往常一样,它工作正常,并在类内使用时显示数据。我错过了什么?
class SetOfStacks
attr_accessor :stacks, :each_stack_size, :stack_number
# Initializing Hash to hold all the stacks and initial stack is created.
def initialize
@stacks = Hash.new # declaring hash to hold all stacks
@each_stack_size = 3 # defining each stack size
@stack_number = 1 # Current stack number
@stacks[@stack_number] = Array.new
end
...
...
end
@obj = SetOfStacks.new
@obj.push(4)
@obj.push(5)
@obj.push(6)
@obj.push(7)
@obj.pop
p @stacks