Don't work an array

Hello, this don't work on ruby 1.9.2. What's matter?

$a = Array.new
for i in 1..5
  for j in 1..8
    $a[i][j] = i*j
  end
end

···

--
Posted via http://www.ruby-forum.com/.

If you look at Array.new, you have to give it a size or something similar.
Otherwise, it assumes it's an array of length 0, in which case $a[i][j]
wouldn't be valid for any values of i and j. See here for more details:
http://ruby-doc.org/core/classes/Array.html#M002150

···

On Sun, Nov 7, 2010 at 6:00 PM, Misha Ognev <b1368810@lhsdv.com> wrote:

Hello, this don't work on ruby 1.9.2. What's matter?

$a = Array.new
for i in 1..5
for j in 1..8
   $a[i][j] = i*j
end
end

--
Posted via http://www.ruby-forum.com/\.

a = Array.new(6){ }
    ==>[, , , , , ]
for i in 1..5
  for j in 1..8
    a[i][j] = i*j
  end
end

···

On Nov 7, 5:00 pm, Misha Ognev <b1368...@lhsdv.com> wrote:

Hello, this don't work on ruby 1.9.2. What's matter?

$a = Array.new
for i in 1..5
for j in 1..8
$a[i][j] = i*j
end
end

--
Posted viahttp://www.ruby-forum.com/.

Hello, this don't work on ruby 1.9.2. What's matter?

Arrays in ruby are dynamic in so far as to its size.

$a = Array.new
for i in 1..5

    $a[i] = # insert; here we specify that the ith element is an array

for j in 1..8
$a[i][j] = i*j
end
end

$a
#=> [nil, [nil, 1, 2, 3, 4, 5, 6, 7, 8], [nil, 2, 4, 6, 8, 10, 12, 14,
16], [nil, 3, 6, 9, 12, 15, 18, 21, 24], [nil, 4, 8, 12, 16, 20, 24,
28, 32], [nil, 5, 10, 15, 20, 25, 30, 35, 40]]

note you have nils in there since arrays in ruby start at 0 index.

nonetheless, that was the long way.

now pls try playing w Array.new especially the block form.

eg,

Array.new(6){|i| 9.times.map{|j| i*j} }
#=> [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8], [0, 2,
4, 6, 8, 10, 12, 14, 16], [0, 3, 6, 9, 12, 15, 18, 21, 24], [0, 4, 8,
12, 16, 20, 24, 28, 32], [0, 5, 10, 15, 20, 25, 30, 35, 40]]

note, no nils.

hth.

kind regards -botp

···

On Mon, Nov 8, 2010 at 7:00 AM, Misha Ognev <b1368810@lhsdv.com> wrote:

The problem is that you haven't initialized the indexes you are accessing.

$a = Array.new
for i in 1..5
  $a[i] = Array.new # **initialize**
  for j in 1..8
    $a[i][j] = i*j
  end
end

Of course, you have several other issues as well. In Ruby, variables
beginning with dollar signs are global. That is probably not what you wanted
to do. To make it a local var, just don't use a sigil.

a = Array.new # now it is local
for i in 1..5
  a[i] = Array.new
  for j in 1..8
    a[i][j] = i*j
  end
end

You still have the issue that your array begins at 1, but Ruby arrays begin
at zero. This means your first index is left nil. You can see what your
object looks like by requiring the pp library, and passing a to the pp
method.

require 'pp'
pp a
# >> [nil,
# >> [nil, 1, 2, 3, 4, 5, 6, 7, 8],
# >> [nil, 2, 4, 6, 8, 10, 12, 14, 16],
# >> [nil, 3, 6, 9, 12, 15, 18, 21, 24],
# >> [nil, 4, 8, 12, 16, 20, 24, 28, 32],
# >> [nil, 5, 10, 15, 20, 25, 30, 35, 40]]

That probably isn't what you wanted. As stated by other users, check out the
block form of Array.new

require 'pp'

a = Array.new 5 do |i|
  Array.new 8 do |j|
    i * j
  end
end

pp a
# >> [[0, 0, 0, 0, 0, 0, 0, 0],
# >> [0, 1, 2, 3, 4, 5, 6, 7],
# >> [0, 2, 4, 6, 8, 10, 12, 14],
# >> [0, 3, 6, 9, 12, 15, 18, 21],
# >> [0, 4, 8, 12, 16, 20, 24, 28]]

It looks like you might be coming from PHP or something. If that is the
case, you should know that PHP's Arrays are sort of a hybrid between arrays
and hash tables, filling both of those roles. In Ruby, they are different
classes. But in Ruby 1.9.x, Hashes are ordered as in PHP (though they won't
presume Array like indexing).

···

On Sun, Nov 7, 2010 at 5:00 PM, Misha Ognev <b1368810@lhsdv.com> wrote:

Hello, this don't work on ruby 1.9.2. What's matter?

$a = Array.new
for i in 1..5
for j in 1..8
   $a[i][j] = i*j
end
end

--
Posted via http://www.ruby-forum.com/\.

Thanks to all.

I used a Josh Cheek advice.

···

--
Posted via http://www.ruby-forum.com/.