Created
February 22, 2012 05:39
-
-
Save EdwinRozario/1881732 to your computer and use it in GitHub Desktop.
Debug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StrArraySort | |
attr_accessor :list | |
def initialize(list) | |
@list = list | |
end | |
def get_the_least_word list | |
mark = list[0].to_s | |
list.each do |word| | |
mark = word.to_s if word < mark | |
end | |
mark | |
end | |
def show | |
puts "Array is #{@list}" | |
end | |
def sort | |
puts @list | |
unsorted_arr = @list | |
sorted_arr = [] | |
while true | |
break if unsorted_arr.length == 0 | |
least = get_the_least_word unsorted_arr | |
unsorted_arr.delete(least) | |
sorted_arr << least | |
end | |
puts sorted_arr | |
sorted_arr | |
end | |
def position element | |
sorted_arr = sort | |
sorted_arr.index(element) | |
end | |
end | |
words_unsorted = ['ant', 'bat', 'cat', 'dog', 'about', 'dot', 'com'] | |
obj = StrArraySort.new(words_unsorted) | |
obj.show # Getting the initialized array | |
puts obj.position('bat') | |
obj.show # Not getting the array even though i haven't modified @list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My colleague helped me up with the above.
class StrArraySort
attr_accessor :list
def initialize(list)
@list = list
end
def get_the_least_word list
mark = list[0].to_s
list.each do |word|
mark = word.to_s if word < mark
end
mark
end
def show
puts "Array is #{@list}"
end
def sort
unsorted_arr = @list.dup # Have to make duplicate of the array to work
sorted_arr = []
while true
break if unsorted_arr.length == 0
least = get_the_least_word unsorted_arr
unsorted_arr.delete(least)
sorted_arr << least
end
puts sorted_arr
sorted_arr
end
def position element
sorted_arr = sort
sorted_arr.index(element)
end
end
words_unsorted = ['ant', 'bat', 'cat', 'dog', 'about', 'dot', 'com']
obj = StrArraySort.new(words_unsorted)
obj.show # Getting the initialized array
puts obj.position('bat')
obj.show # Not getting the array even though i haven't modified @list