title: Trying to write a sorting program with ruby - stack level too deep (system stack error)
body: <p>I'm reading Chris Pine's book "Learn to Progam" (it's about Ruby). Right now I'm trying to write a program that sorts words. Unfortunately I'm stuck with: <code>stack level too deep (system stack error) in line 16</code>, which, if i Googled correctly means that there is an infinite loop, but I don't know why.</p>
  
  <p>Here's the code:  </p>
  
  <pre><code>words = []
  wordss = []
  word = 'word'
  i = 0
  k = 0
  
  def sortw array
    i = 0
    if    (array.length == 1) || (array.length == 0)
    else sort array, [], [], i
    end
    return array
  end
  
  def sort unsorted, unsort, sorted, i
    k = 0 # The error should be here, according to command prompt
    while i &lt; unsorted.length
  
      while (unsorted[i] &lt; unsorted[k])
        if    k &lt; unsorted.length
          k = k + 1
        elsif k == unsorted.length
          sorted.push unsorted[i]
        else unsort.push unsorted[i]
        end
      end
  
      i = i + 1
      sort unsorted, unsort, sorted, i
    end
  
    if unsort.length != 1
      i = 0
      sort unsort, [], sorted, i
    else sorted.push unsort[0]
    end
  
    return sorted
  end
  
  puts 'type one word per line...'
  puts 'typing enter on an empty line sorts the inputted words'
  
  while word != ''
    word = gets.chomp
    words = words.push word
  end
  
  wordss = (sortw words)
  
  puts 'your words'
  puts words
  puts 'sorted here'
  puts wordss
  </code></pre>