Hi im starting to learn programming and I'm stuck in this

puts "enter as much words as you like and i will sort them"
words = []
while true
	words.push gets.chomp 
	if words.last == ""
		words.delete(words.last)
		break
	end
end
sorted = []
def sort unsorted, sorted, num, var
	while true
		if unsorted[num] < unsorted[var]
			var = var + 1 
			if var + 1 == unsorted.length
				sorted.push unsorted[num]
				unsorted[num].delete
				var = 1
			end
		else 
			ya = unsorted[num]
			unsorted[num].delete
			unsorted.push ya
			var = 1
		end
		break if unsorted == ""
	end
end
puts sort [words], [sorted], 0, 1

in line 13 it gives me this error "in `sort': undefined method `<' for #<Array:0x1d331c0> (NoMethodError)"
if I take that out of mi method it works
so mi question is: is there a way to make the main methods like "<" "<=" ">" ">=" to work inside mi custom method or to call them or to define them again?????
please anwer mi question even if the code can be done in some other way i whould really apreciate it

My guess is that you do not know what is stored in unsorted . From the error message it looks like it is an array of arrays while you are expecting it to be a simple array of values. It is difficult to tell for sure without understanding the calling context.

From the error message it looks like it is an array of arrays while you are expecting it to be a simple array of values.

well I tested it again and the error mesage changed to this "sort.rb:13:in `sort': undefined method `<' for ["hello", "please", "stand", "rush", "fruit", "at", "d"]:Array (NoMethodError)" "from sort.rb:29:in `<main>'"
the code is still the same as the first one so I don't think that's the problem but anyway thank you.
just another thing do you know a way make the main or premade methods like "<" "<=" ">" ">=" to work inside mi custom method or to call them, or define them again?
it's not just for this code but to know for further use thanks and sorry if i'm bothering you with this newbie question

You are still getting an undefined method error on an array - that is exactly what NoMethodError means. Consider the following:

#!/usr/bin/env ruby

if [1,2,3,4,5] < [4,5,6,7,8,9]
    puts "Less than"
end

Running that gives me the following exception

undefined method `<' for [1, 2, 3, 4, 5]:Array (NoMethodError)

Look familiar?

You are not getting what you expect in that function.

#!/usr/bin/env ruby

if [1,2,3,4,5] < [4,5,6,7,8,9]
puts "Less than"
end

thats right
but in my code what im doing its something like this

arr1 = [1,2,3,4,5]
arr2 = [4,5,6,7,8,9]
num1 = 0
num2 = 2
if arr1[num1] < arr2[num2]
    puts "Less than"
end

and the code above runs just fine
what i dont understand is why the one above runs fine and mine not if im doing basicaly the same

I just found what was it: words is an array, but I put it inside another array when I call sort so now I just pass it like this

puts "enter as much words as you like and i will sort them"
words = []
while true
	words.push gets.chomp 
	if words.last == ""
		words.delete(words.last)
		break
	end
end
sorted = []
def sort unsorted, sorted, num, var
	while true
		if unsorted[num] < unsorted[var]
			var = var + 1 
			if var + 1 == unsorted.length
				to_sorted = unsorted[num]
				sorted.push to_sorted
				unsorted.delete to_sorted
				var = 1
			end
		else 
			to_last = unsorted[num]
			unsorted.delete to_last
			unsorted.push to_last
			var = 1
		end
		break if unsorted == ""
	end
end
puts sort words, sorted, 0, 1

but now it gives me this error and I dont know why
sort.rb:13:in `<': comparison of String with nil failed
(ArgumentError)
from C:/sort.rb:13:in `sort'
from C:/sort.rb:30:in `<main>'
please help

At some point you are accessing beyond the stored elements in your array. var likely becomes unsorted.size and you then use it as an index into unsorted causing a nil result.

you know its way to dificult for me I'll try it again later when im better programmer so anyways thankyou

I've always found that working through tough problems made me a better programmer. We all face problems when starting out that challenge us; as you progress in you abilities you start to look for these situations to break up the monotony. At least that is the way it has happened for me.

Well that's right for everything but I'll try another program and then go back to this one just to put my mind on another thing for a while but anyway thank you a lot for the help it was very usefull

The program is done !!!! :) i was only checking it for the last time before going to do something else and I found the solution well the problem and the solution.
the program now looks like this:

puts "Hello", "Just enter as many words as you wish and I will sort them"
words = []
while true 
	words.push gets.chomp
	if words.last == ""
		words.delete(words.last)
		break
	end
end
sorted = []
def sort unsorted, sorted, num, var
	while true
		break if unsorted.length == 0
		if unsorted.length == 2
			if unsorted[num] < unsorted[1]
				allmost = unsorted[num]
				last_one = unsorted[1]
				sorted.push allmost
				sorted.push last_one
				unsorted.delete (allmost)
				unsorted.delete (last_one)
				break
			elsif unsorted[num] > unsorted[1]
				last_one = unsorted[1]
				allmost = unsorted[num]
				sorted.push unsorted[1]
				sorted.push unsorted[num]
				unsorted.delete (last_one)
				unsorted.delete (allmost)
			end
		end
		if unsorted.length > 1
			if unsorted[num] < unsorted[var]
					var += 1
					if var + 1 == unsorted.length
						si = unsorted[num]
						sorted.push si
						unsorted.delete si
						var = 1
					end
			else
				to_last = unsorted[num]
				unsorted.delete (to_last)
				unsorted.push to_last
				var = 1 
			end
			
		elsif 	unsorted.length == 1 
			done = unsorted.num 
			sorted.push done
			break
		end
	end
	puts sorted
end
puts sort words, sorted, 0, 1

Thank you for your help it was really usefull

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.