High & Low Numbers From A String (Ruby)
<p>Good evening,</p>

<p>I'm trying to solve a problem on Codewars:</p>

<p><em>In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.</em></p>

<p><em>Example:</em></p>

<pre><code>high_and_low("1 2 3 4 5")  # return "5 1"
high_and_low("1 2 -3 4 5") # return "5 -3"
high_and_low("1 9 3 4 -5") # return "9 -5"
</code></pre>

<p><em>Notes:</em></p>

<p><em>All numbers are valid Int32, no need to validate them.</em>
<em>There will always be at least one number in the input string.</em>
<em>Output string must be two numbers separated by a single space, and highest number is first.</em></p>

<p>I came up with the following solution however I cannot figure out why the method is only returning "542" and not "-214 542". I also tried using #at, #shift and #pop, with the same result.</p>

<p>Is there something I am missing? I hope someone can point me in the right direction. I would like to understand why this is happening.</p>

<pre><code>def high_and_low(numbers)
    numberArray = numbers.split(/\s/).map(&amp;:to_i).sort
    numberArray[-1]
    numberArray[0]
end

high_and_low("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6")
</code></pre>

<p><em>EDIT</em></p>

<p>I also tried this and receive a failed test "Nil":</p>

<pre><code>def high_and_low(numbers)
  numberArray = numbers.split(/\s/).map(&amp;:to_i).sort
  puts "#{numberArray[-1]}" + " " + "#{numberArray[0]}"
end
</code></pre>