From: Josef 'Jupp' SCHUGT Date: 2003-12-03T23:23:42+09:00 Subject: Re: New to ruby--trouble with initializing arrays Hi! * Van Jacques; 2003-12-02, 23:31 UTC: > "Robert Klemme" wrote in message news:... [Object oriented life] > This is much more sophiscated than my program (with help from > another poster in a similar thread here in comp.lang.ruby), but I > used the > > row % size > > method for dealing with the edges also. The solution is not as sophisticated as it seems. Simply imagine that you number the fields like this (use fixed width font):
: +--+--+--+--+--+--+
: | 0| 1| 2| 3| 4| 5|
: +--+--+--+--+--+--+
: | 6| 7| 8| 9|10|11|
: +--+--+--+--+--+--+
: |12|13|14|15|16|17|
: +--+--+--+--+--+--+
: |18|19|20|21|22|23|
: +--+--+--+--+--+--+
: |24|25|26|27|28|29|
: +--+--+--+--+--+--+
: |30|31|32|33|34|35|
: +--+--+--+--+--+--+
If you now store them one after another going one to the right/left when assuming 0 and 5 are neighbors and current position is n results in (n +/- 1) modulo 6. For the row the same works - with the only difference that the result of the operation must be multiplied by the distance of the chunks representing a whole row (6 in this case). Perhaps you should try the following C program: #include void main() { int i, j, a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) printf("a[%d][%d] = %d\n", i, j, a[i][j]); } Surprise, surprise: That really works (even though the initalization at first sight does not make much sense) and results in: a[0][0] = 1 a[0][1] = 2 a[0][2] = 3 a[1][0] = 4 a[1][1] = 5 a[1][2] = 6 a[2][0] = 7 a[2][1] = 8 a[2][2] = 9 In short: C does it that way, too - because it is the most efficient one. > As a beginner, I'm not used to defining my own classes or def > statements, but that will come with time--I hope :-) That will come much faster than you expect. Suggestions: Take a look at rcalc - it starts with Array and ends with a quite powerful scientific calculator (it does not use UPN for lazyness but because I prefer it over Infix notation...) - http://rcalc.rubyforge.org/ Josef 'Jupp' Schugt -- for i in $(seq 1 9); do rm /bin/cat done