title: Ruby: undefined method '>' for nil:NilClass (NoMethodError)
body: <p>Currently working on a binary search algorithm for Ruby, but am running into an error when comparing the middle array element with n.</p>
  <p>Code Below:</p>
  <pre><code>def b_search(n, arr)
      middle = arr.length / 2
      first = 0
      last = arr.length - 1
  
      while first &lt;= last
          middle = first + last / 2
          if arr[middle] == n
              return true
          elsif arr[middle] &gt; n
              last = middle - 1
          else
              first = middle + 1
          end
      end
      false
  end
  
  nums = [1, 2, 3, 4, 5]
  target = 4
  
  if b_search(target, nums)
      puts &quot;Target Found&quot;
  else
      puts &quot;Target Not Found&quot;
  end
  </code></pre>
  <p>The error evidently happens in the while loop at the</p>
  <p><code>elsif arr[middle] &gt; n</code></p>
  <p>line, but I'm not sure why. Any help would be appreciated.</p>