Operators/IF statement not working as expected with negative numbers


This is my code:

puts "Enter a number: "
a = gets.chomp
puts "Enter a second number: "
b = gets.chomp

if a > b
  puts "The first number is bigger!"
  a < b
  puts "The second number is bigger!"
  puts "The two numbers are equal!"

When a == -1 and b == -2 it returns a is greater than b.

Is there a problem with my code?



no, -1 is greater then -2.


Apologises; I miss-typed. It’s actually return that b is greater than a

The below is an example of output I am seeing:

Enter a number: 
Enter a second number: 
The second number is bigger!

I very well may just be confusing myself.


a and b variable are of type string, string comparing numbers gives unexpected behavior.


Ah I see. I guess this will be covered in a later lesson/I will have to research it.

Thank you for your help and your quick replies.


just convert a and b to integers, then all should work fine


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.