Compare in Ruby

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Ruby

To compare numbers in Ruby, you can use comparison operators. Below are some examples:

1. Greater than:

The greater than operator in Ruby is ">" (without quotes). It returns true if the left operand is greater than the right operand, and false otherwise. For example:

2 > 1 # returns true
1 > 2 # returns false

2. Less than:

The less than operator in Ruby is "<" (without quotes). It returns true if the left operand is less than the right operand, and false otherwise. For example:

1 < 2 # returns true
2 < 1 # returns false

3. Greater than or equal to:

The greater than or equal to operator in Ruby is ">=" (without quotes). It returns true if the left operand is greater than or equal to the right operand, and false otherwise. For example:

2 >= 2 # returns true
3 >= 2 # returns true
1 >= 2 # returns false

4. Less than or equal to:

The less than or equal to operator in Ruby is "<=" (without quotes). It returns true if the left operand is less than or equal to the right operand, and false otherwise. For example:

2 <= 2 # returns true
1 <= 2 # returns true
3 <= 2 # returns false

5. Equality:

The equality operator in Ruby is "==" (without quotes). It returns true if the operands are equal, and false otherwise. For example:

2 == 2 # returns true
2 == 3 # returns false

6. Inequality:

The inequality operator in Ruby is "!=" (without quotes). It returns true if the operands are not equal, and false otherwise. For example:

2 != 3 # returns true
2 != 2 # returns false