Doing chris pine learn to program - chapter 7 challenge feedback

I think you should take a look at some methods of sorting and choose
which you think is the best.


I chose bubble because it is the simplest and I came up with this, what
do you think?:

  1 class WordList
  2 @words
  3
  4 def initialize
  5 @words = []
  6 end
  7
  8 def add word
  9 @words.push word
10 end
11
12 def show
13 @words
14 end
15
16 def sort
17 sorted = @words
18 length = sorted.length
19
20 begin
21 swap = false
22
23 sorted.each_index do |e|
24 next_key = e + 1
25
26 next if next_key >= length
27
28 if sorted[e] > sorted[next_key]
29 sorted[e], sorted[next_key] = sorted[next_key], sorted[e]
30 swap = true
31 end
32 end
33 end while swap
34
35 sorted
36 end
37 end
38
39 class Main
40 @words
41
42 def initialize
43 @words = WordList.new
44
45 write_some_words
46 show_words
47 show_sorted
48 end
49
50 def write_some_words
51 msg = "Type something:"
52
53 puts msg
54
55 while((word = gets.chomp) != '') do
56 puts msg
57 @words.add word
58 end
59 end
60
61 def show_words
62 p @words.show
63 end
64
65 def show_sorted
66 p @words.sort
67 end
68 end
69
70 Main.new

···

-----Ursprüngliche Nachricht-----
Gesendet: Wednesday, 14 August 2013 um 18:47:41 Uhr
Von: "Rafael M." <lists@ruby-forum.com>
An: ruby-talk@ruby-lang.org
Betreff: Re: doing chris pine learn to program - chapter 7 challenge feedback
--
Posted via http://www.ruby-forum.com/.