Forum: Ruby Pine's tutorial "Word Sorter" exercise

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Randy Shipp (Guest)
on 2007-02-19 20:46
(Received via mailing list)
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

SonOfLilit (Guest)
on 2007-02-19 20:54
(Received via mailing list)
#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!
This topic is locked and can not be replied to.