From: Robert Klemme Date: 2003-12-03T21:07:05+09:00 Subject: Re: New to ruby--trouble with initializing arrays "Van Jacques" schrieb im Newsbeitrag news:70ae81fd.0312021355.67a1f746@posting.google.com... > "Robert Klemme" wrote in message news:... > > > > > > Does this help: > > > > Slightly improved to tackle the borders. > > > > class Board > > def initialize(size=8) > > @size = 8 > > @values = Array.new( size * size, 0 ) > > end Sorry, I put an error in here: > > def [](x,y) > > @values[pos(x,y)]=v > > end Should've read: def [](x,y) @values[pos(x,y)] end > > > > def []=(x,y,v) > > @values[pos(x,y)]=v > > end > > > > protected > > def pos(x,y) > > ( x % @size ) * @size + ( y % @size ) > > end > > end > > > > > board = Board.new > > > tmp = board.dup > > > # manipulate tmp > > > board = tmp > > > > robert > > 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 following initial configuration > goes a long time before oscillating (I think), and I recommend running it > for anyone who is interested. > > As a beginner, I'm not used to defining my own classes or def statements, > but that will come with time--I hope :-) Sure enough! You'll be surprised how easy that is with Ruby once you get used to OO thinking. The things (read: objects) get a live of their own. Kind regards robert