Mac some quick ruby noob help

x704

macrumors regular
Original poster
Apr 15, 2006
118
0
Hi, I am trying to figure out how to add to a multi-dimensional array. I have:

array = Array.new
array.concat( [['a','b','c'], [[1,2,3],[2,3,4],[3,4,5]], ['D','E','F'],'Z'] )
puts array
array[3][0][0] = 'G' #my problem

As you might see I am trying to add 'G' to the cell [3][0][0]so the array would then look like:

[['a', 'b', 'c'], [[1, 2, 3], [2, 3, 4], [3, 4, 5]], ['D', 'E', 'F'], 'Z', [['G']] ]

unfourtionaly the book that I have does not cover multi-dimensional arrays. And Googleing did not help me.

EDIT: after a sec I noticed I already put something in the cell array[3], so I changed it to
array[4][0][0] = 'G' #and I get the error
"undefined method `[]' for nil:NilClass (NoMethodError)"
 

demallien

macrumors regular
Oct 13, 2005
137
0
I'm no Ruby expert, but...

if I had to guess what it's doing, Ruby is interpretting the forth element of your array as a string 'Z'... The first 0 in the array[3][0][0] is hence the byte representing the first character of the string. The attempt to then de-reference this byte by putting the second [0] results in Ruby trying to access protected memory.

to fix the problem:
1) you don't need to do the concat. You could just declare your array as
array = [['a','b','c'], [[1,2,3],[2,3,4],[3,4,5]], ['D','E','F'],'Z']
Ruby will figure out all by it's little lonesome that array is actual an array...

2) to add an element, you could use the concat operator. So, to do what you want, try array.concat('G')

The solution will be something along those lines.... As I said, I'm not an expert in Ruby
 
Comment

kainjow

Moderator emeritus
Jun 15, 2000
7,958
5
x704 said:
EDIT: after a sec I noticed I already put something in the cell array[3], so I changed it to
array[4][0][0] = 'G' #and I get the error
"undefined method `[]' for nil:NilClass (NoMethodError)"
It's nil because you can't assign an object to a nil object... meaning, you must define array[4] first before using it.

I'm no Ruby expert either, but like demallien said, you can use concat to add an item, or assign it directly like you're trying to do:
Code:
array[4] = [['G']]
# or...
array.concat [['G']]
 
Comment

DrEasy

macrumors member
Jan 12, 2004
99
0
I'm a Ruby noob myself, but I believe you can use the "push" method to add an element to the back of your array (basically like a Vector in Java). Also, you seem to want to add [['G']], not 'G' (it's not the same thing!), so you have to modify your code accordingly.

Try:

array.push([['G']])
 
Comment

x704

macrumors regular
Original poster
Apr 15, 2006
118
0
Ok, I was wondering because I plan on using a 2D array for a log file analyzer I still plan on making in ruby :). I need to be able to take a lline from the log file, break it up and append it to the array for further analysis in my program.

This would be my first (usefull) program in ruby but not my first program. I like to think I am decent in Java.. it's just that ruby handles arrays so much differently. The reason I was doing an array like the one above is to get a feel for ruby arrays before I make the program and have it not work.

Thanks for your help so far, I'll have to try the sugested stuff you three offered.
 
Comment

x704

macrumors regular
Original poster
Apr 15, 2006
118
0
ok, I think I somewhat figured out ruby arrays...
To do what I wanted to do I need to create the dimension explicetly.
I did so by

array[4] = [[]]
array[4][0][2] = 'G'

That seems to work fine. I just now had a bit of time to try a couple of things and thought I would let you guys know how it went.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.