为什么我不能设置多维数组的值?

时间:2012-01-03 02:25:18

标签: ruby arrays multidimensional-array

从该数组创建和获取值的工作正常。我可以调整它的大小并用true值填充它而没有问题。

  class Solution

    def initialize
      @solution_matrix = [[[]]]
    end

    def [](receiver, provider, moment)
        return @solution_matrix[receiver][provider][moment]
    end

    def []=(receiver, provider, moment, value)
      puts "making " + receiver.to_s + " : " +  provider.to_s + " : " +moment.to_s + " -> " + value.to_s
      @solution_matrix[receiver][provider][moment] = value
    end

当我尝试修改任何值时,问题就开始了。 @solutionSolution的实例:

     @solution[2,3,15]=false;

控制台输出是:

     making 2 : 3 : 15 -> false

一切似乎都没问题,但是当我尝试时:

  def list_disconnected 
    number_of = 0
    for receiver in 0...@problem.number_of_receivers
      for provider in 0...@problem.number_of_providers
        for moment in 0...@problem.number_of_moments
          if (@solution[receiver,provider,moment]==false)
            puts receiver.to_s + " : " +  provider.to_s + " : " +moment.to_s
            number_of += 1
          end
        end
      end
    end  
    puts number_of
  end

它表明moment 15中的所有值都设置为false。用:

number of receivers: 40
number of providers: 6
number of moments: 50

我得到240个假细胞。

0 个答案:

没有答案