title: Towers of Hanoi: Recursive Explanation in Ruby
body: <p><strong>NOTE: I understand the recursive solution; but I don't see how the code is achieving it, as I can't follow the step-by-step of the code's executing.</strong></p>
  
  <p>I'm having a difficult time understanding the recursive loop for the Towers of Hanoi. I've commented and tried various tactics to see how the procedure is operating, but I can't seem to grasp how the loops are operating and how the rings are being directed.</p>
  
  <pre><code>  def move(rings, from, destination, other)
      if rings == 1
        ring = from.pop
        p "Move ring #{ring} from #{from} to #{destination}"
        destination.push ring
      else
        move(rings-1, from, other, destination)
        move(1, from, destination, other)
        move(rings-1, other, destination, from)
      end
    end
  </code></pre>
  
  <p>Here's the output:</p>
  
  <pre><code>"Move ring 1 from a to c"
  "Move ring 2 from a to b"
  "Move ring 1 from c to b"
  "Move ring 3 from a to c"
  "Move ring 1 from b to a"
  "Move ring 2 from b to c"
  "Move ring 1 from a to c"
  </code></pre>
  
  <p>I've looked at various explanations, however, I don't see one explaining how the loops are being executed. For instance, I can't figure out why in the cases where the rings are even, the first step places ring 1 from a to b, while for all odd number of rings, it's from a to c, as shown above.</p>
  
  <p>I understand the logic behind the solution, that in order to move n number of rings to destination while using an auxiliary peg requires that you first move n-1 number of rings to the auxiliary peg followed by moving the nth ring to the destination and repeating the first procedure, again. But, I'm unsure how the directions for placement are changing, e.g. I don't see how a block is going from c to b when the call to the move method, above, doesn't seem to mention c to b.</p>
  
  <p>Thank you, in advance, for your time and help.</p>
  
  <p>Also, if you have any suggestions for helping track the process of code executions in Ruby, please let me know. I'd love to hear some of your insights to how you'd troubleshoot instances where you're unsure how things are being executed.</p>
  
  <p>Eager to hear your answers :)</p>