Don't work an array

M

Misha Ognev

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[j] = i*j
end
end
 
A

Andrew Wagner

[Note: parts of this message were removed to make it a legal post.]

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[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
 
W

w_a_x_man

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[j] = i*j
  end
end


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

botp

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 =3D Array.new
for i in 1..5

$a =3D [] # insert; here we specify that the ith element is an arra=
y
=A0for j in 1..8
=A0 =A0$a[j] =3D i*j
=A0end
end


$a
#=3D> [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} }
#=3D> [[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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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[j] = i*j
end
end

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

$a = Array.new
for i in 1..5
$a = Array.new # **initialize**
for j in 1..8
$a[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 = Array.new
for j in 1..8
a[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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top