-
-
Save DouglasAllen/1868386 to your computer and use it in GitHub Desktop.
#!/usr/local/rvm/rubies/ruby-1.9.3-p125/bin/ruby | |
# a neat trick to block with. | |
=begin | |
[code ruby] | |
It never hurts to document your code | |
vars | |
a_word = "" # a nill string | |
words = [] # an empty array for input and sorting | |
words2 = [] # an empty array for output and sorting | |
j = 0 # inner iterater counter | |
b=word = '' # was used but not now | |
i = 0 # outter iterator | |
[code] | |
=end | |
#~ puts "Type your word and then press <Enter>. When finished just press <Enter>." | |
puts "debug line 17 #{words = []}" | |
#~ a_word = "" | |
#~ while a_word != "" | |
#~ a_word = gets.chomp | |
#~ if a_word != "" | |
#~ words.push a_word | |
#~ i = i + 1 | |
#~ end | |
#~ end | |
#~ puts i # nill? okay, we're out of scope range. | |
#~ # what if i = nill? | |
#~ words = [] | |
words = %w( ant bat cat dog about dot com ruby loops) | |
i = words.length | |
puts "debug line 33 #{i}" | |
words2 = [] | |
puts "debug line 37 #{words}" | |
until i == words2.length | |
a_word = words[0] | |
@sort_index = 0 | |
#=begin | |
# not yet working, why? | |
for j in 0..i | |
puts "debug line 48 #{words[j]}" | |
puts "debug line 49 #{a_word}" | |
if a_word > words[j] # compare the two words | |
a_word = words[j] | |
@a_index = j | |
puts "debug line 54 #{@sort_index}" | |
end | |
end | |
#=end | |
puts "debug line 59 #{@sort_index}" | |
words2.push a_word # push smallest word onto new array | |
words.delete_at(@sort_index) # delete the lowest element in the original array | |
end | |
puts "debug line 64 #{words2}" | |
=begin | |
this is my test run | |
kb9agt@mepis1:~/Documents$ ./ok_scope | |
debug line 17 [] | |
debug line 33 7 | |
debug line 37 ["ant", "bat", "cat", "dog", "about", "dot", "com"] | |
debug line 48 ant | |
debug line 49 ant | |
debug line 48 bat | |
debug line 49 ant | |
debug line 48 cat | |
debug line 49 ant | |
debug line 48 dog | |
debug line 49 ant | |
debug line 48 about | |
debug line 49 ant | |
debug line 54 0 | |
debug line 48 dot | |
debug line 49 about | |
debug line 48 com | |
debug line 49 about | |
debug line 48 | |
debug line 49 about | |
./ok_scope:51:in `>': comparison of String with nil failed (ArgumentError) | |
from ./ok_scope:51:in `block in <main>' | |
from ./ok_scope:46:in `each' | |
from ./ok_scope:46:in `<main>' | |
kb9agt@mepis1:~/Documents$ | |
=end |
words_unsorted = ['ant', 'bat', 'cat', 'dog', 'about', 'dot', 'com']
words_sorted = []
def get_the_least_word(list)
mark = list[0]
list.each do |word|
mark = word if word < mark
end
return mark
end
while true
break if words_unsorted.length == 0
least = get_the_least_word(words_unsorted)
words_unsorted.delete(least)
words_sorted << least
end
puts words_sorted
require 'json'
words_unsorted = ['ant', 'bat', 'cat', 'dog', 'about', 'dot', 'com']
words_sorted = []
def get_the_least_word(list)
mark = list[0]
list.each do |word|
mark = word if word < mark
end
return mark
end
while true
break if words_unsorted.length == 0
least = get_the_least_word(words_unsorted)
words_unsorted.delete(least)
words_sorted << least
end
json_data = JSON.generate(words_sorted)
puts json_data
I really had to beg them to share it even though it does not completely function. So please don't criticize it. Take our challenge and then share with us what you found out about why it doesn't work and not how it can use more ruby style.
Ruby is open source so feel free to look into the class libraries for more insight. Hint: Enumerations,
Array, Exceptions. Browse the actual code. That is what is making ruby so powerful.
Developers are constantly improving on it by making things like gems and extensions to existing libraries.
So have fun.
Learn to make your own tools to help you become an awesome programmer.
I actually think that Edwin cheated. Isn't any .each method part of the Enumerations class that sort came from?
This could be debatable.