Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:40:03+00:00 2026-05-29T19:40:03+00:00

Why does the code below set my @groups array to: @groups=[[[24, 25, 26], [27,

  • 0

Why does the code below set my @groups array to:

@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]]]

the @groups.inspect shows the weird stuff above: why?
the puts statement in the code below shows the expected 0..35. @groups is initiated with nil for all elements when this code starts.

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

Running this babe produces below: (inspect at the end)

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:40:04+00:00Added an answer on May 29, 2026 at 7:40 pm

    The probjem is here:

    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
    

    In this method you have e_groups variable holding array of length @n_groups and in each (0..@n_events-1).each do |i| you reinitialize it’s array elements with nil and then assign reference to the same Array instance to groups[i]. This is equal to @groups = [e_groups, e_groups, e_groups].

    And then in your init_groups method you reassign values of the same array 3 times:

    # 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]]]
    

    The solution:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: The code below does indeed work as expected, and accomplishes the code I
Why does the code below return true only for a = 1? main(){ int
Why does the code below result in org.datanucleus.exceptions.NucleusUserException: Object Manager has been closed? The
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Why does the below code give Seg. Fault at last line? char* m=ReadName(); printf(\nRead
In the code below, why does the open function work but the close function
I have to code below - works great in IE and Opera, but does
In the below code sample, what does {0:X2} mean? This is from the reflection
I have few questions. First of all does code below violates law of Demeter?
I have the below code that does not seem to work at all :(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.