creating a 2d array

A

Adam Akhtar

Hi im just coming to ruby from C albeit after many years of no
programming. Im trying the simple matrix problem to get used to dealing
with arrays in ruby.

How do you create a blank 2d array?

I want to ask the user for the number of rows and columns and then
create a blank 2d array using those dimensions. From there I'd populate
it with the users desired data.

Cheers
 
T

Todd Benson

Hi im just coming to ruby from C albeit after many years of no
programming. Im trying the simple matrix problem to get used to dealing
with arrays in ruby.

How do you create a blank 2d array?

I want to ask the user for the number of rows and columns and then
create a blank 2d array using those dimensions. From there I'd populate
it with the users desired data.

Cheers

x, y = 2, 3 #you get these values from the user
m = [] #initializing m for scope reasons
x.times { m << Array.new( y ) } # adding new arrays to m

That should get you started. Keep in mind that arrays are just
ordered lists of objects. You can have an ordered list of any object
(including other arrays). The [] index call can be chained. So to
access this 2d array at 0,0 you would do m[0][0].

hth,
Todd
 
T

tho_mica_l

x, y = 2, 3 #you get these values from the user
m = [] #initializing m for scope reasons
x.times { m << Array.new( y ) } # adding new arrays to m

Or maybe:

Array.new(x) {Array.new(y)}
 
C

Cameron McBride

Hi im just coming to ruby from C albeit after many years of no
programming. Im trying the simple matrix problem to get used to dealing
with arrays in ruby.

There is a matrix class in the stdlib. But if you're doing anything
numeric, I'd recommend using NArray.
How do you create a blank 2d array?

I want to ask the user for the number of rows and columns and then
create a blank 2d array using those dimensions. From there I'd populate
it with the users desired data.

Todd's suggestion will work, but I prefer:
rows, cols = 2,3
mat = Array.new(rows) { Array.new(cols) }

Cameron
 
A

Adam Akhtar

Todd's suggestion will work, but I prefer:
rows, cols = 2,3
mat = Array.new(rows) { Array.new(cols) }

Cameron

Why do you use the {}? Arent they used when your creating hashes.
 
C

Clifford Heath

Adam said:
Why do you use the {}? Arent they used when your creating hashes.

This is a block (like do...end), not a Hash, in this context.
The block is passed to the Array constructor and which calls it
with each index to yield a value for the elements of the outer
array being constructed.

Clifford Heath.
 
A

Adam Akhtar

Ok i think i kinda understand. Could you tell me if this is something
specific to arrays or is it used for other objects as well? If I wanted
to learn more about this in a book what should i look up in the
index/contents?

Your help is much apppreciated!!
 
C

Clifford Heath

Adam said:
Ok i think i kinda understand. Could you tell me if this is something
specific to arrays or is it used for other objects as well? If I wanted
to learn more about this in a book what should i look up in the
index/contents?

Worth reading in detail: <http://ruby-doc.org/core/classes/Array.html>.
Use IRB to enter examples of each usage shown, to make sure you understand
it. When you've read that, move on to Hash, String and Enumerable:

<http://ruby-doc.org/core/classes/Hash.html>,
<http://ruby-doc.org/core/classes/String.html>,
<http://ruby-doc.org/core/classes/Enumerable.html>.

Apart from knowing the Ruby syntax, 90% of all your code is covered by
those four pages. When you've worked through them all once, do it again
from the top, and you'll learn still more.

Clifford Heath.
 

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

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
BettinaPol

Latest Threads

Top