title: Ruby recursion: getting some weird output.!
body: <pre><code>class Array
    def total_zeros index = entries.length-1, total = 0
      p "total entries = #{entries.length}"
      i = 0
      if index &gt;= 0
        p "in if block"
        i = i+1
        total_zeros index-1, total
      end
      p "hello #{i}"
    end
  end
  
  a = [0,1,2]
  p a.total_zeros
  </code></pre>
  
  <p>This is my sample output: </p>
  
  <pre><code>"total entries = 3"
  "in if block"
  "total entries = 3"
  "in if block"
  "total entries = 3"
  "in if block"
  "total entries = 3"
  "hello 0"
  "hello 1"
  "hello 1"
  "hello 1"
  nil
  </code></pre>
  
  <p>Can anybody please help me to understand this code? I am not able to understand why the last line "hello" is printed four times. It is a recursion and "hello" should be printed only once having <code>i</code> equal to 4.</p>