For interest, before I bought the book I did this exercise for the 
online tuorial.  That was before I knew about recursives,  here is what 
I came up with... it works, but it's not pretty



words_array = []
puts 'Please enter any word'
word = gets.chomp

while word != ''
  words_array.push word
  puts 'Great, please enter another word'
  word = gets.chomp
end

sorted_array = []

words_array.each do |word|
  if word.to_s.downcase > sorted_array.last.to_s.downcase
    #add word to the end of sorted_array
    sorted_array.push word

  else
    index = (sorted_array.length) - 1
    initial_number = index
    temp_array = []

    #check word against next word in sorted_array and repeat
    while word.to_s.downcase < sorted_array[index].to_s.downcase
      #add the sorted_array to a temp_array up to that point
      temp_array[index] = sorted_array[index]
      #erase words in sorted_array up to that point
      sorted_array.pop
      index = index - 1
    end

    #add the word to sorted_array
    sorted_array.push word

    #add one by one the words from temp_array to sorted_array
    while index < initial_number
      sorted_array.push temp_array[index + 1]
      index = index + 1
    end
  end
end

puts sorted_array


-- 
Posted via http://www.ruby-forum.com/.