# Elementary Question on Variables

**URL:** <https://www.ruby-forum.com/t/elementary-question-on-variables/156011>\
**Category:** Rails\
**Created:** [December 19, 2008, 7:59am UTC](https://www.ruby-forum.com/t/elementary-question-on-variables/156011 "2008-12-19T07:59:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jordantheous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jordantheous](https://www.ruby-forum.com/u/jordantheous)\
**Post date:** [December 19, 2008, 7:59am UTC](https://www.ruby-forum.com/t/elementary-question-on-variables/156011/1 "2008-12-19T07:59:10Z")

</div>

hey all,

this is a great community and I’m glad I found it…also, this is my  
first post.

In the code example below taken from Chris P. Learn to Program RoR  
book, I’m curious why there is (what looks like to me) a hanging  
variable: “size”. It is the third line from the bottom. I understand  
how recursion is working here, but I’m not sure why “size” is just  
sitting there at the bottom. I removed the line and re-ran the program  
and still got the correct result. Am I missing something?

–

M = ’ land ’  
o = ’ water ’  
world = [[o,o,o,o,o,o,o,o,o,o,o],  
[o,o,o,o,M,M,o,o,o,o,o],  
[o,o,o,o,o,o,o,o,M,M,M],  
[o,o,o,M,o,o,o,o,o,M,o],  
[o,o,o,M,o,M,M,o,o,o,o],  
[o,o,o,o,M,M,M,M,o,o,o],  
[o,o,o,M,M,M,M,M,M,M,o],  
[o,o,o,M,M,o,M,M,M,o,o],  
[o,o,o,o,o,o,M,M,o,o,o],  
[M,M,o,o,o,M,o,o,o,o,o],  
[o,o,o,o,o,o,o,o,o,o,o]]

def continent\_size world, x, y  
if x \> 10 && y \> 10  
size = 0  
else  
if world[y][x] != ’ land ’  
return 0  
end  
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  
#THIS LAST “SIZE” IS WHAT I’M TALKING ABOUT ABOVE. YOU CAN REMOVE IT  
AND THE #PROG STILL RUNS CORRECTLY

end

## puts continent\_size(world, 5, 5)

Thanks in advance for any thoughts you have.

Best,  
Jordan

---

<div class="post-metadata">

**Author:** ![jordantheous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jordantheous](https://www.ruby-forum.com/u/jordantheous)\
**Post date:** [December 19, 2008, 8:12am UTC](https://www.ruby-forum.com/t/elementary-question-on-variables/156011/2 "2008-12-19T08:12:34Z")

</div>

On 19 Dec 2008, at 06:59, Jordan Theous wrote:

> sitting there at the bottom. I removed the line and re-ran the  
> program  
> and still got the correct result. Am I missing something?

The return value of method in ruby is the result of the last statement  
(or whatever you pass to return)  
Sticking size at the bottom of that method makes that the result. If  
you remove that then the return value will be the previous statement  
ie size = size + continent\_size(world, x+1, y+1)

which will generate the same return value, however the intention is  
not perhaps as explicit as if you have size on its own at the end of  
the method.

Fred

---

<div class="post-metadata">

**Author:** ![jordantheous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a88e57/32.png) [@jordantheous](https://www.ruby-forum.com/u/jordantheous)\
**Post date:** [December 19, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/elementary-question-on-variables/156011/3 "2008-12-19T16:40:17Z")

</div>

Thanks Fred, your comments were very helpful.
