0

I'm new to writing in Ruby and I have this assignment for my Programming Languages class where I have to implement Mergesort into Ruby such that the user can enter an array of their own choice of numbers and end with a -1. I thought I had everything written in correctly, there are no bugs being reported, but the program doesn't print anything out.

Here's the important part of the code:

puts "Please enter as many numbers as you would like followed by -1"

Many_Numbers = Array.new
  x_1 = '-1'
while gets != x_1
  Many_Numbers.push gets
end

sorted = merge_sort(Many_Numbers)
puts "SORTED CORRECTLY: #{sorted == Many_Numbers.sort}\n\n#{Many_Numbers}\n\n#{sorted}"

Like I said, nothing is printed out, not even what is provided in the puts methods, so I have nothing to present for an error. What am I doing wrong, here?

EDIT: I edited the code after I had an idea to improve this part of the code but I still got nothing. This is what I changed

puts "Please enter as many numbers as you would like followed by -1"

Many_Numbers = Array.new

input = gets
while input != -1
  case response
    when input != -1
      Many_Numbers.push(input)
    when input == -1
    end
  end
6
  • You ought to supply a minimal working example. puts functionality is not relevant to merge sort -- you can minimize the example by removing unrelated merge sort code. More importantly, you'll need to include how you're executing this code. Other people running this code aren't going to be able to reproduce the problem. To start, what happens when you execute: ruby -e "puts 'hey'"? Commented Sep 14, 2020 at 4:38
  • I edited the question, and writing out ` ruby -e "puts 'hey'" ` does not work. It causes an error to occur. ` syntax error, unexpected tIDENTIFIER, expecting do or '{' or '(' ruby -e puts "hey" ^~~~ ` is what it prints out. Commented Sep 14, 2020 at 4:46
  • Run ruby -e "puts 'hey'" in shell, not in Ruby. What are you doing to execute your code? Commented Sep 14, 2020 at 5:28
  • I'm using sublime text 3 to run my code. Commented Sep 14, 2020 at 5:32
  • Sublime Text 3 is a text editor -- it isn't (normally) used to execute code. It sounds to me like you may have a different issue entirely, but don't know enough to ask the correct/relevant question. Commented Sep 14, 2020 at 5:44

1 Answer 1

0

You have a couple of problems with your input code.

  1. gets returns all the user input, which includes the newline. So, your loop condition is comparing "-1" to "-1\n" and hence will never end. Calling .chomp on the input will fix that.
  2. You are calling gets twice for each valid number -- once in the loop condition and once when you actually push a value into your array. This causes the loss of one of every two entries. Using a loop do construct with a break condition can fix that problem.
END_OF_LIST = '-1'

puts "Please enter as many numbers as you would like followed by -1"

Many_Numbers = Array.new
loop do
  val = gets.chomp
  break if val == END_LIST
  
  Many_Numbers.push val
end

The good news is your merge sort method appears to be working once you sort out your input woes.

Sign up to request clarification or add additional context in comments.

4 Comments

I still end up with nothing printing out. Is there anything else you would suggest?
OK, I can confirm that your original code is broken and with my changes it executes as I assume you expect it to. But, if you are not even seeing the initial prompt string, then you have a problem with either ruby on your system or with Sublime. Check out stackoverflow.com/questions/31185417/… for possible help; I don't use Sublime so can't help you.
Ok, I managed to run the program, but I end up with this error: 2: from Fross_Charles_PA2_Mergesort.rb:37:in <main>' 1: from Fross_Charles_PA2_Mergesort.rb:37:in loop' Fross_Charles_PA2_Mergesort.rb:39:in block in <main>': undefined local variable or method num' for main:Object (NameError)
That’s my bad; a cut and paste error. Where you see num change it to val to match the variable assignment above it. I fixed it in the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.