March 2009
Intermediate to advanced
194 pages
4h
English
How you could do it:
puts 'Give me some words, and I will sort them:' |
words = [] |
while true |
word = gets.chomp |
if word == '' |
break |
end |
words.push word |
end |
puts 'Sweet! Here they are, sorted:' |
puts words.sort |
| <= | Give me some words, and I will sort them: |
| => | banana |
| => | apple |
| => | cherry |
| => | |
| <= | Sweet! Here they are, sorted: |
apple | |
banana | |
cherry |
How I would do it:
puts 'Give me some words, and I will sort them:' |
words = [] |
while true |
word = gets.chomp |
break if word.empty? |
words << word |
end |
puts 'Sweet! Here they are, sorted:' |
puts words.sort |
| <= | Give me some words, and I will sort them: |
| => | banana |
| => | apple |
| => | cherry |
| => | |
| <= | Sweet! Here they are, sorted: |
apple | |
banana | |
cherry |