Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Debug
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
@EdwinRozario

This comment has been minimized.

Copy link
Owner Author

@EdwinRozario EdwinRozario commented Feb 22, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment