Pine's tutorial "Word Sorter" exercise


On Apr 1 2006, 8:12 pm, Jan_K removed_email_address@domain.invalid wrote:

puts input.sort

To reset slightly, this is an exercise that tells you to accept words
from the user until they just hit Enter, then alphabetize the list.

My problem with the exercise is that the sort method alphabetizes
capital letters before lowercase letters. Therefore, if input ==
[ant, bear, Zebra], the output of

puts input.sort



…which is clearly not what most users will expect. Short of
changing the line in the program to read

input.push gets.chomp.downcase

(thus altering the entered words in a possibly undesirable way), how
can we get the sort to work the way we might expect? Thanks in



#sort and #sort_by accept blocks that tell how to do the comparison, so:

puts input.sort_by{|a| a.downcase}

is the simplest solution.

PLUG: The RubyMentor project is looking for newbies to help!