2012-02-09 104 views
4

爲什麼下面的代碼設置我@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。當此代碼啓動時,@groups對於所有元素都以nil啓動。

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 
    @[email protected]_tasks/@n_events 
    [email protected]_groups*@n_events 
    [email protected]_tasks-even 
    @group_sizes = [] 

    added_groups=n_expanded_groups 
    ([email protected]_groups-1).each do 

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

     [email protected]_events 

     @group_sizes << x 
    end 
    end 

    def create_groups 
    @groups=Array.new(@n_events) 
    e_groups=Array.new(@n_groups) 
    ([email protected]_events-1).each do |i| 
     ([email protected]_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]]]> 
+0

貴陣列有一個默認值? – 2012-02-09 11:45:20

+4

或者說,告訴我們你是如何初始化你的@groups數組 – 2012-02-09 11:49:49

+0

一個不錯的。當我剛接觸Ruby時,這也發生在我身上。 – 2012-02-09 12:29:57

回答

6

的probjem是在這裏:

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

在該方法中必須長度@n_groupse_groups變量保持陣列並在每個([email protected]_events-1).each do |i|您重新初始化它的數組元素nil,然後分配參考到相同的陣列實例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) 
    ([email protected]_events-1).each do |i| 
     e_groups=Array.new(@n_groups) # re-initialize e_groups 
     ([email protected]_groups-1).each do |j| 
      e_groups[j]=Array.new(@group_sizes[j]) 
     end 
     @groups[i]=e_groups 
    end 
end