我面临着在 Ruby 中创建自己的排序方法的挑战。这是我目前得到的:
sort_me = []
current_word = 'a'
sorted = []
puts "Enter a series of words or characters to be sorted! One word at a time, press enter when complete."
while current_word != ''
current_word = gets.chomp
sort_me.push current_word
end
puts ''
puts "You have chosen the values:"
puts sort_me
sort_me.each do |word|
sort_me.each do |word2|
if word > word2
sorted.push word
else
end
end
end
puts "They have been sorted thusly:"
puts sorted
嵌套的 .each 部分出了什么问题?在我看来,它似乎应该使用 > 操作数将“sort_me”中的每个值与其他值进行比较,然后将它们从最高值的单词开始添加到“排序”中。谢谢!