From: Mariano Kamp Date: 2006-10-10T04:02:17+09:00 Subject: Re: Learn to Program, by Chris Pine Hi Mike, I don't know the book and that is probably true for a lot of the people here. Maybe you should include the full source code. Especially interesting would be to see the line numbers as the interpreter clearly indicates where the problem occurs. As a totally wild guess ... The error message says that the right side of an addition is nil. So looking at your code, "size" seems to be set, maybe continent_size does return nil? What I also think is strange is that "world" seems to be a two dimensional array when you first mention it (world[x][y] = 'counted land'), but later on you pass it to continent size along with two parameters. This might be ok, if continent_size defines three parameters, like this: def continent_size(world, x, y); end and you use x and y to access the array then?! I don't know about the conditional. What are you trying to check? That both x and y are between 1 and 10? That should work ... Even though you wouldn't need the braces as it all of the conditionals need to be true for the whole expression to become true. This would be different if you use "or". Hope that helps! Cheers, Mariano On Oct 9, 2006, at 4:46 AM, Mike Agres wrote: > I'm having some issues modifying the Civ continent example from ch. 10 > to account for coordinates that are outside the array. I added the > following line: > > if ((y <= 10 && y > 0) && (x <= 10 && x > 0)) > > before... > > # So, first we count this tile... > size = 1 > world[y][x] = 'counted land' > > > # ...then we count all of the > # neighboring eigth tiles (and, > # of course, their neighbors via recursion) > size = size + continent_size(world, x-1, y-1) #4,4 > size = size + continent_size(world, x , y-1) #5,4 > size = size + continent_size(world, x+1, y-1) #6,4 > size = size + continent_size(world, x-1, y ) #4,5 > size = size + continent_size(world, x+1, y ) #6,5 > size = size + continent_size(world, x-1, y+1) #4,6 > size = size + continent_size(world, x , y+1) #5,6 > size = size + continent_size(world, x+1, y+1) #6,6 > size > end > > As a result, I get the following error: > > TypeError: nil can't be coerced into Fixnum > > method + in civilization.rb at line 35 > method continent_size in civilization.rb at line 35 > method continent_size in civilization.rb at line 35 > method continent_size in civilization.rb at line 36 > method continent_size in civilization.rb at line 35 > method continent_size in civilization.rb at line 40 > method continent_size in civilization.rb at line 39 > method continent_size in civilization.rb at line 41 > method continent_size in civilization.rb at line 39 > method continent_size in civilization.rb at line 41 > method continent_size in civilization.rb at line 39 > method continent_size in civilization.rb at line 36 > method continent_size in civilization.rb at line 35 > at top level in civilization.rb at line 48 > > This error appears regardless of whether the arguments I pass are > in 5,5 > (as in the example Chris Pine wrote out) or something like 10,10 > (which > is on the edge of the 'continent'). > > Is that the right conditional statement? Could it be in the wrong > place > in the method? > > -- > Posted via http://www.ruby-forum.com/. >