Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The exception is occurring because you are trying to compare nil. You get a different exception when nil is on the left.</p> <pre><code>'1' &lt; nil #=&gt; scratch.rb:1:in `&lt;': comparison of String with nil failed (ArgumentError) nil &gt; '1' scratch.rb:1:in `&lt;main&gt;': undefined method `&gt;' for nil:NilClass (NoMethodError) </code></pre> <p>Your code gets into this situation when the <code>left</code> or <code>right</code> array is empty (ie all of its elements have been added to A already). Presumably, this is why you had originally added the if-statements for <code>nanol == 0</code> and <code>nanor == 0</code> (ie to handle when one of the arrays is empty).</p> <p>Your if-statements have a couple of issues:</p> <ol> <li>You do need the <code>nanol == 0</code> and <code>nanor == 0</code> statements</li> <li>The three if-statements are always run, even though only one would apply in an iteration</li> <li><code>nanol</code> and <code>nanor</code> are never re-calculated (ie they will never get to zero)</li> <li>When the left and right values are equal, you don't actually add anything to the <code>A</code> array</li> </ol> <p>The inside of your iteration should be:</p> <pre><code>smallest_left = left.min smallest_right = right.min nanol=left.length nanor=right.length if nanol == 0 #Handles left no longer having values A &lt;&lt; right.delete_at(right.index(smallest_right) || right.length) elsif nanor == 0 #Handles right no longer having values A &lt;&lt; left.delete_at(left.index(smallest_left) || left.length) elsif smallest_left &lt; smallest_right A &lt;&lt; left.delete_at(left.index(smallest_left) || left.length) elsif smallest_left &gt; smallest_right A &lt;&lt; right.delete_at(right.index(smallest_right) || right.length) else #They are equal so take one A &lt;&lt; left.delete_at(left.index(smallest_left) || left.length) end </code></pre> <p>You will still have an issue (no error, but unexpected results) when your list has an odd number of elements. But hopefully that answers your question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload