Creating Arrays Through a Loop

Discussion in 'Ruby' started by Squawk Boxed, Feb 1, 2010.

  1. Squawk Boxed

    Squawk Boxed Guest

    I was wondering if it was possible to create a series of arrays through
    a loop. This doesn't work (I think):

    for num in 1..10
    array_num = Array.new
    end
    --
    Posted via http://www.ruby-forum.com/.
    Squawk Boxed, Feb 1, 2010
    #1
    1. Advertising

  2. 2010/2/1 Squawk Boxed <>:
    > I was wondering if it was possible to create a series of arrays through
    > a loop. =A0This doesn't work (I think):
    >
    > for num in 1..10
    > =A0array_num =3D Array.new
    > end


    Well, it *does* work - only you loose references to a newly created
    Array immediately. You can try some of these variants:

    arrs =3D []
    10.times { arrs << Array.new }

    arrs =3D Array.new(10) { Array.new }
    arrs =3D (1..10).map { Array.new }

    Cheers

    robert


    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Feb 1, 2010
    #2
    1. Advertising

  3. Squawk Boxed wrote:
    > I was wondering if it was possible to create a series of arrays through
    > a loop. This doesn't work (I think):
    >
    > for num in 1..10
    > array_num = Array.new
    > end


    Of course that doesn't work. You're setting the same variable
    (array_num) each time. What you should do is use an array of arrays:

    array_of_arrays = Array.new(10) {Array.new}

    Best,
    --
    Marnen Laibow-Koser
    http://www.marnen.org


    --
    Posted via http://www.ruby-forum.com/.
    Marnen Laibow-Koser, Feb 1, 2010
    #3
  4. Squawk Boxed wrote:
    > I was wondering if it was possible to create a series of arrays through
    > a loop. This doesn't work (I think):
    >
    > for num in 1..10
    > array_num = Array.new
    > end


    It works fine, but it will always assign the new array to the same
    variable.

    One idea is to create an array, and then add arrays as elements.

    main_array = Array.new
    (1..10).times { main_array << Array.new }

    This may do what you want, but there may also be a more Ruby-ish way,
    depending on why you need those arrays.
    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Feb 1, 2010
    #4
  5. 2010/2/1 Aldric Giacomoni <>:
    > Squawk Boxed wrote:
    >> I was wondering if it was possible to create a series of arrays through
    >> a loop. =A0This doesn't work (I think):
    >>
    >> for num in 1..10
    >> =A0 array_num =3D Array.new
    >> end

    >
    > It works fine, but it will always assign the new array to the same
    > variable.
    >
    > One idea is to create an array, and then add arrays as elements.
    >
    > main_array =3D Array.new
    > (1..10).times { main_array << Array.new }


    Either use (1..10).each or use 10.times - but (1..10).times won't work:

    irb(main):002:0> (1..10).times {|*a| p a}
    NoMethodError: undefined method `times' for 1..10:Range
    from (irb):2
    from /opt/bin/irb19:12:in `<main>'


    Cheers

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Feb 1, 2010
    #5
  6. Robert Klemme wrote:
    > 2010/2/1 Aldric Giacomoni <>:
    >>
    >> One idea is to create an array, and then add arrays as elements.
    >>
    >> main_array = Array.new
    >> (1..10).times { main_array << Array.new }

    >
    > Either use (1..10).each or use 10.times - but (1..10).times won't work:
    >


    Whoops! That's what I get for crossing two thoughts.


    --
    Posted via http://www.ruby-forum.com/.
    Aldric Giacomoni, Feb 1, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,536
    Chris Smith
    Jun 17, 2004
  2. Roedy Green
    Replies:
    3
    Views:
    415
    Mike Schilling
    Sep 13, 2008
  3. Philipp
    Replies:
    21
    Views:
    1,109
    Philipp
    Jan 20, 2009
  4. Mitchell Gould
    Replies:
    8
    Views:
    150
    Mitchell Gould
    Jul 14, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    351
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page