Martin DeMello wrote: > So what happens when you go off the end of the array? Calling array[n] > where n is out of bounds will return nil, as expected... > > array[1][100] # => nil > > which is equivalent to > > a = array[1] #=> [..., ..., ...] > a[100] #=> nil > > But if you go out of bounds in the other dimension > > array[100][1] # => undefined method `[]=' for nil:NilClass > (NoMethodError) > > because what you're in effect doing is > > a = array[100] #=> nil > a[1] # => trying to call [] on nil, which raises the error > > martin Understood. Does that mean it should work if I wanted to wrap top and bottom, and cap left and right, rather than the other way around? I've tried this but it also fell over with the same error. I would think that if array[100][1] won't work for the reasons you give above, array[1][100] should work, returning nil? Perhaps I could write a custom method for the nil class? Other than that, I don't know how to get around this - maybe investigate NArray, or add rows top and bottom that are invisible, and absorb all top/bottom out of bounds queries. Dave Howell wrote: > Well, for what it's worth, it could have been 'eleganted' far more > unreadably than this. {grin} As a beginner, I don't mind elegant code, as long as the longer version is also presented to compare. If you can't do this, long version. And if not that, then, yes, elegant is fine and I'll just have to nut it out - better than nothing at all. Your code was only abstracted a little from mine, so understandable even to me, although for 5 seconds I thought, eek. > I've worked with hex grids before... Thx. That's helful. I will go through it properly once I get my capping. It makes sense at first glance. >> Array2D[x,y,1] = "X " > > I don't consider myself a hard-core Ruby expert, so I wouldn't be > surprised if there was a bug in my code. On the other hand, I'll bet > part of the problem is that you appear to be confusing a Class with an > instance. You can't (or shouldn't normally be able to) assign anything > to "Array2D". My new array is not called Array2D in my program. My mistake to write it as I did. Should have used new_array perhaps. new_array = Array2D(16,8) new_array[6,0,1] = "X " You did leave out a few brackets, and forgot one of the -1's next @height. I fixed those and as far as I could see there wasn't anything wrong with it, but it's not like I'd know. -- Posted via http://www.ruby-forum.com/.