my ruby
So I was reading a thread at MudLab and Tyche had posted a list of Ruby resources. Now I’ve never really coded anything but, as I was curious about Ruby, I checked some of the links out and ten freakin hours later I wrote my first Ruby program!
This is an exercise from the really cool Learn To Program by Chris Pine, I highly recommend that site. Great job Chris! In my post all-nighter vibe I post the code with no shame:
# declare variables and arrays
word = 'start'
unsortedList = []
sortedList = []
lowercase = []
trues = 0
# say hello
puts 'Hello. Type a word, press [ENTER] after every word, [ENTER] when done: '
# get the input
while word != ''
word = gets.chomp
unsortedList.push word
end
# downcase the list so we can sort it
unsortedList.each do |i|
lowercase.push i.downcase
end
# sort the list
while lowercase.length > 0
lowercase.each do |i|
lowercase.each do |j|
if (i &lt = j)
trues = trues + 1
end
end
if (trues == lowercase.length)
sortedList.push i
lowercase.delete(i)
trues = 0
else
trues = 0
end
end
end
# see the sorted list
puts '-' * 60
puts 'Your sorted list, dude:'
puts sortedList
# voila
I wrote this trying not to use any other reference but Chris’s tutorial up to the point of the exercise, but I admit I couldn’t hack it and I looked up a library reference for the delete method (which I probably could have just guessed at and got right). Now this is not perfect, I know, I know, blah blah. But it’s my first ruby! It’s all mine! (insert maniacal laughter here…)
And I gotta figure out how to format that better…code tags don’t do so well — fixed.
Subscribe to Kooneiform



