Hi, I'm totally new to ruby and web development (with only
html/css/javascript and some php experience).
I want to get into RoR development but would like to get a good
understanding of ruby before doing railstutorial.org.
I'm following Chris Pine's online tutorial for ruby and just spent a few
hours on this one challenge at chapter 7.. you take
input from a user, store it all into an array then have to print it back
out in alphabetical order. But without the sort method.
I purposely didn't look up any other methods or for help, to see if this
could be accomplished only with what was taught up to that point (no
writing def, or using other methods/loops that weren't covered)
Here's the resulting code, it looks messy.. but feedback would be
appreciated.
words = []
endProg = nil
while endProg != ''
words.push gets.chomp
endProg = words.last
end
words.pop
num1 = -1
num2 = 0
more = 0
value1 = nil
value2 = nil
while more < words.length
words.each do |sort|
if num1 < (words.length - 1)
if value1 != 'reset'
value2 = value1
value1 = sort
if value2 != nil and value1 != nil and value1 != value2
if value2 > value1
words[num1] = value1
words[num2] = value2
#Here, I skip over the rest of the each loop and do the
#while loop again
#so I don't over write any of the old values
num1 = num1 + words.length
# reducing the more because we need an extra iteration
more = more - 1
end
end
else
value1 = sort
end
num1 = num1 + 1
num2 = num2 + 1
end
end
words = words
num1 = -1
num2 = 0
sort = nil
value1 = 'reset'
value2 = 'reset'
more = more + 1
end
puts
puts words
puts
Obviously this could be done more efficiently with more operators and
methods, but could this have been done better by using only the basics
(do, while, if, else, pop, push, last)
Thanks
···
--
Posted via http://www.ruby-forum.com/.