
June 14th, 2007, 12:57 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
You can either declare an array using Array.new, or assign it to an empty array instance []:
Code:
words = Array.new
or
words = []
As for the rest of the code, it is not too hard to write
Code:
#!/usr/bin/env ruby
words = Array.new
# Get list of words from the user
word = gets.chomp
while word != ''
words.push word
word = gets.chomp
end
# Sort the array
sortedwords = words.sort
# Print it out
sortedwords.each do |word|
puts word
end
and if you don't want to declare a separate sorted array, you can use words.sort! instead (note the ! at the end of the function name. In ruby, functions ending with ! end up modifying the object directly, instead of returning a copy)
Code:
#!/usr/bin/env ruby
words = Array.new
# Get list of words from the user
word = gets.chomp
while word != ''
words.push word
word = gets.chomp
end
# Sort the array
words.sort!
# Print it out
words.each do |word|
puts word
end
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|