Return behavior for recursion in Ruby
<p>I was working on a simple recursive method to implement Euclid's algorithm in Ruby, and find myself stuck figuring out how to return the desired value once the base case is reached. Here's what I have to far:</p>

<pre><code>def euclid_alg(larger,smaller)
  if larger % smaller == 0 &amp;&amp; smaller != 1
    return smaller
  else
    puts 'calling self'
    euclid_alg(smaller, (larger % smaller))
    puts 'executed section after call'
  end
  puts "made it here #{smaller} #{larger}"
  nil
end
puts euclid_alg(100,15)
</code></pre>

<p>And the output:</p>

<pre><code>calling self
calling self
executed section after call
made it here 10 15
executed section after call
made it here 15 100
</code></pre>

<p>Note there is no output from "puts euclid_alg(100,15)" which I was expecting to return the greatest common divisor of 100 and 15, 5.</p>

<p>In order to troubleshoot, I replaced the <code>return smaller</code> on line 3 with <code>puts smaller</code>. The new output was: </p>

<pre><code>calling self
calling self
5
made it here 5 10
executed section after call
made it here 10 15
executed section after call
made it here 15 100
</code></pre>

<p>The addition of the "made it here 5 10" to the console output makes it clear that the return statement is breaking out of the function call, but not "parent calls."</p>

<p>How can I do recursion better?</p>