将对象放入数组将返回一个空数组

问题描述 投票:0回答:1

问题

我有一个称为queue的方法,该方法属于Board类,该方法返回一个空数组。我在将修改后的对象推送到此数组时遇到麻烦。当前它返回一个空数组。

def queue
  queue = []
end

功能说明

我有一个rules方法,该方法属于Board类,有条件地调用queue_for_birthqueue_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_birthqueues_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 ...

arrays ruby queue each block
1个回答
0
投票

向板级添加实例变量queue解决了此问题

© www.soinside.com 2019 - 2024. All rights reserved.