Marcelo Alvim wrote: > Hey, People. > > I'm a newbie, as the subject implies, and some questions about Ruby > raised when I was trying to solve this quiz. > > I'm not trying to create a very fast solution, I'm only trying to > learn mor of the language. So I'm trying a simple depth-first search > approach. > > I'm going to have this two-dimensional array representing the board. > At each place of the board, I'll probably put the number that is > currently on that place. > > The problem, now, is that I chose not to assume the board as a torus, > but as a flat square. So, I'm doing that simple math going from one > square to the next: if it's an horizontal move, I add 3 or -3 to X; > if it's vertical, apply the same to Y; if it's the diagonal, add 2 or > -2 to both of them. Well, you already know that. Unfortunately, Ruby's > behavior of wrapping arrays over using their negative indices, which I > like very much on most cases, is kind of annoying now, since I want to > invalidate access to negative indices. > > So, I thought of some approaches for "solving" this problem of mine, > and would like to know your opinions: > > 1) I could create a class that extends Array, or encapsulates one, and > define the #[] and #[]= methods. Then those methods would check the > negative indices and return nil if they were negative. > > 2) I could alias_method those methods for the actual Array classe and > override the original ones, also checking indices inside (Though I > think if I use this approach in anything but my simple quiz solution, > I would have to return the Array class back to normal afterwards). > > 3) I could check indices on my code everytime I need, outside of any > Array class. > > 4) You guys could teach me a super-cool Ruby-esque way to do that that > I can't even think about right now. > > Thanks for your patience, > Alvim. Or, you could initialize the array with some string (I used '.' in mine, to match the quiz) and then check for nil. -Justin