Ruby operators - comparison
Ruby Operators
Operator Description
Subtraction - Subtracts right hand operand from left hand operand
Multiplication - Multiplies values on either side of the operator / Division - Divides left hand operand by right hand operand % Modulus - Divides left hand operand by right hand operand and returns remainder ** Exponent - Performs exponential (power) calculation on operators
Ruby Assignment Operators
Combined Operator Equivalent x += y x = x + y x -= y x = x - y x /= y x = x / y x *= y x = x * y x %= y x = x % y x **= y x = x ** y
x = 10 x += 5 # Assigns a value of 15 to variable x (the same as x = x + 5)
Comparison Operators
Comparison Operator Description == Tests for equality. Returns true or false .eql? Same as ==. != Tests for inequality. Returns true for inequality or false for equality < Less than. Returns true if first operand is less than second operand. Otherwise returns false
Greater than. Returns true if first operand is greater than second operand. Otherwise returns false.
= Greater than or equal to. Returns true if first operand is greater than or equal to second operand. Otherwise returns false. <= Less than or equal to. Returns true if first operand is less than or equal to second operand. Otherwise returns false. <=> Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second.
irb(main):006:0> 1 == 1 => true
irb(main):019:0> 10 <=> 10 => 0
irb(main):020:0> 10 <=> 9 => 1