我有一个称为queue的方法,该方法属于Board
类,该方法返回一个空数组。我在将修改后的对象推送到此数组时遇到麻烦。当前它返回一个空数组。
def queue
queue = []
end
我有一个rules
方法,该方法属于Board
类,有条件地调用queue_for_birth
或queue_for_death
def rules(cell)
if something
cell.queue_for_birth
else something_else
cell.queue_for_death
end
end
在也属于game_logic
类的Board
方法中包含的每个块中多次调用此方法。每次调用rules
,并且每次调用queues_for_birth
或queues_for_death
时,都应将该单元格实例添加到称为队列的数组中]
def game_logic(cells) cells.each_with_index do |cell,index| if index == 0 rules(cell) #push to the queue end if index == 1 rules(cell) #push to the queue end ...
这里是将单元格添加到队列的逻辑所在的地方,它属于
Cell
类,该类继承自Board
def queue_for_birth(queue) @will_survive = true queue << self end
但是,每当我调用board.queue时,都会返回一个空数组。
=> []
问题我有一个名为queue的方法,它属于Board类,它返回一个空数组。我在将修改后的对象推送到此数组时遇到麻烦。当前,它返回一个空数组。 def ...
向板级添加实例变量queue
解决了此问题