Note that there are some explanatory texts on larger screens.

plurals
  1. POChris Pine Ruby ch 10, recursion
    primarykey
    data
    text
    <p>I have been trying to learn ruby using the book 'learn to program' by Chris Pine. I was actually getting excited when going through the book until I got to chapter 10 and the examples used. Now this chapter alone and its examples have completely deflated all of my excitement to continue with this book. In this example I have completely no idea how its trying to count the tiles, or why he uses world [y],[x] when the method was defined with the attribute of continent_size world, x,y? Im not sure how the recursion in this example works. Can someone shed some more light onto this example as to what the author was actually trying to do?</p> <pre><code>M = 'land' o = 'water' world = [ [o,o,o,o,o,M,o,o,o,o,o], [o,o,o,o,M,M,o,o,o,o,o], [o,o,o,o,o,M,o,o,M,M,o], [o,o,o,M,o,M,o,o,o,M,o], [o,o,o,o,o,M,M,o,o,o,o], [o,o,o,o,M,M,M,M,o,o,o], [M,M,M,M,M,M,M,M,M,M,M], [o,o,o,M,M,o,M,M,M,o,o], [o,o,o,o,o,o,M,M,o,o,o], [o,M,o,o,o,M,M,o,o,o,o], [o,o,o,o,o,M,o,o,o,o,o]] def continent_size world, x ,y if x &lt; 0 or x &gt; 10 or y &lt; 0 or y &gt; 10 return 0 end if world[y][x] != 'land' return 0 end size = 1 world [y][x] = 'counted land' size = size + continent_size(world, x-1, y-1) size = size + continent_size(world, x , y-1) size = size + continent_size(world, x+1, y-1) size = size + continent_size(world, x-1, y ) size = size + continent_size(world, x+1, y ) size = size + continent_size(world, x-1, y+1) size = size + continent_size(world, x , y+1) size = size + continent_size(world, x+1, y+1) size end puts continent_size(world, 5, 5) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload