title: Why should I assign nil when declaring a variable instead of giving it a base value?
body: <p>I've been doing a couple of practice problems in Ruby and this has come up often.</p>
  
  <p>Let's say i'm trying to solve for the third greatest integer in an array of integers</p>
  
  <pre><code>def third_greatest(nums)
    first = 0
    second = 0
    third = 0
    i = 0
  
    while(i &lt; nums.length)
       if nums[i] &gt; first
         third = second
         second = first
         first= nums[i]
       elsif nums[i] &gt; second
         third = second
         second = nums[i]
       elsif nums[i] &gt; third
         third = nums[i]
       end
     i+=1
   end
   return third
  end
  </code></pre>
  
  <p>This code does satisfy the tests cases provided</p>
  
  <p>The solution given however initializes the variables with nil and makes an extra check in each if statement if the variable is nil</p>
  
  <pre><code>def third_greatest(nums)
    first = nil
    second = nil
    third = nil
  
    idx = 0
    while idx &lt; nums.length
      value = nums[idx]
      if first == nil || value &gt; first
        third = second
        second = first
        first = value
      elsif second == nil || value &gt; second
        third = second
        second = value
      elsif third == nil || value &gt; third
       third = value
      end
     idx += 1
    end
  
    return third
  end
  </code></pre>
  
  <p>In what cases is my code incorrect? Which code is better practice? It feels more cumbersome in case 2 though I can imagine there are edge-cases where my solution might not work.</p>