具有奇怪值的ruby数组

时间:2012-02-09 11:35:46

标签: ruby arrays loops

为什么下面的代码将我的@groups数组设置为:

@groups=[[[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]], [[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]], [[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]]]

@ groups.inspect显示了上面的奇怪内容:为什么? 下面代码中的puts语句显示了预期的0..35。在此代码启动时,@groupsnil为所有元素启动。

class Os_tasks

  def set_n_tasks(n)
    @n_tasks=n
  end

  def set_n_events(n)
    @n_events=n
  end


  def create_group_sizes
    @n_groups=@n_tasks / @n_events
    even=@n_groups*@n_events
    n_expanded_groups=@n_tasks-even
    @group_sizes = []

    added_groups=n_expanded_groups
    (0..@n_groups-1).each do

      x=0
      if added_groups > 0
        x=1
        added_groups=added_groups-1
      end

      x=x+@n_events

      @group_sizes << x
    end
  end

  def create_groups
    @groups=Array.new(@n_events) 
    e_groups=Array.new(@n_groups)
    (0..@n_events-1).each do |i|
      (0..@n_groups-1).each do |j|
        e_groups[j]=Array.new(@group_sizes[j])
      end
      @groups[i]=e_groups
    end
  end

  def init_groups

    x=0
    @groups.each_index do |i|
      @groups[i].each_index do |j|
        @groups[i][j].each_index do |k|
          @groups[i][j][k]=x
          puts "i:" + i.to_s + ", j:" + j.to_s + ", k:" + k.to_s + ", x:" + x.to_s + " @groups[" + i.to_s + "][" + j.to_s + "][" + k.to_s + "]: " + @groups[i][j][k].to_s
          x = x + 1  

        end

      end
    end

  end

end

gtask = Os_tasks.new

puts "Number of task groups: "
gtask.set_n_tasks(gets.chomp.to_i)
puts "Number of events: "
gtask.set_n_events(gets.chomp.to_i)

gtask.create_group_sizes
gtask.create_groups
gtask.init_groups
puts gtask.inspect

运行这个宝贝产生如下:(最后检查)

Number of tasks:
12
Number of events:
3
i:0, j:0, k:0, x:0 @groups[0][0][0]: 0
i:0, j:0, k:1, x:1 @groups[0][0][1]: 1
i:0, j:0, k:2, x:2 @groups[0][0][2]: 2
i:0, j:1, k:0, x:3 @groups[0][1][0]: 3
i:0, j:1, k:1, x:4 @groups[0][1][1]: 4
i:0, j:1, k:2, x:5 @groups[0][1][2]: 5
i:0, j:2, k:0, x:6 @groups[0][2][0]: 6
i:0, j:2, k:1, x:7 @groups[0][2][1]: 7
i:0, j:2, k:2, x:8 @groups[0][2][2]: 8
i:0, j:3, k:0, x:9 @groups[0][3][0]: 9
i:0, j:3, k:1, x:10 @groups[0][3][1]: 10
i:0, j:3, k:2, x:11 @groups[0][3][2]: 11
i:1, j:0, k:0, x:12 @groups[1][0][0]: 12
i:1, j:0, k:1, x:13 @groups[1][0][1]: 13
i:1, j:0, k:2, x:14 @groups[1][0][2]: 14
i:1, j:1, k:0, x:15 @groups[1][1][0]: 15
i:1, j:1, k:1, x:16 @groups[1][1][1]: 16
i:1, j:1, k:2, x:17 @groups[1][1][2]: 17
i:1, j:2, k:0, x:18 @groups[1][2][0]: 18
i:1, j:2, k:1, x:19 @groups[1][2][1]: 19
i:1, j:2, k:2, x:20 @groups[1][2][2]: 20
i:1, j:3, k:0, x:21 @groups[1][3][0]: 21
i:1, j:3, k:1, x:22 @groups[1][3][1]: 22
i:1, j:3, k:2, x:23 @groups[1][3][2]: 23
i:2, j:0, k:0, x:24 @groups[2][0][0]: 24
i:2, j:0, k:1, x:25 @groups[2][0][1]: 25
i:2, j:0, k:2, x:26 @groups[2][0][2]: 26
i:2, j:1, k:0, x:27 @groups[2][1][0]: 27
i:2, j:1, k:1, x:28 @groups[2][1][1]: 28
i:2, j:1, k:2, x:29 @groups[2][1][2]: 29
i:2, j:2, k:0, x:30 @groups[2][2][0]: 30
i:2, j:2, k:1, x:31 @groups[2][2][1]: 31
i:2, j:2, k:2, x:32 @groups[2][2][2]: 32
i:2, j:3, k:0, x:33 @groups[2][3][0]: 33
i:2, j:3, k:1, x:34 @groups[2][3][1]: 34
i:2, j:3, k:2, x:35 @groups[2][3][2]: 35

#<Os_tasks:0x28b1428 @n_tasks=12, @n_events=3, @n_groups=4, @group_sizes=[3, 3, 3, 3], @groups=[[[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]], [[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]], [[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]]]>

1 个答案:

答案 0 :(得分:6)

问题在这里:

def create_groups
    @groups=Array.new(@n_events)
    e_groups=Array.new(@n_groups)
    (0..@n_events-1).each do |i|
        (0..@n_groups-1).each do |j|
            e_groups[j]=Array.new(@group_sizes[j])
        end
        @groups[i]=e_groups
    end
end

在此方法中,您拥有e_groups变量持有数组@n_groups,并在每个(0..@n_events-1).each do |i|中使用nil重新初始化其数组元素,然后指定引用< / strong> 到同一个数组实例groups[i]。这等于@groups = [e_groups, e_groups, e_groups]

然后在init_groups方法中,您将同一个数组的值重新分配3次:

# i = 0
[[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]], [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]], [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]]
# i = 1
[[[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]
# i = 2
[[[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]], [[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]], [[24, 25, 26], [27, 28, 29], [30, 31, 32], [33, 34, 35]]]

解决方案:

def create_groups
    @groups=Array.new(@n_events)
    (0..@n_events-1).each do |i|
        e_groups=Array.new(@n_groups) # re-initialize e_groups
        (0..@n_groups-1).each do |j|
            e_groups[j]=Array.new(@group_sizes[j])
        end
        @groups[i]=e_groups
    end
end