Ruby Operators

Ruby has a wide variety of operators. Below is a tutorial covering many of the most commonly used ones.

  • Arithmetic Operators: Perform mathematical operations.
a = 10
b = 20

puts b + a  # Addition: Outputs 30
puts b - a  # Subtraction: Outputs 10
puts b * a  # Multiplication: Outputs 200
puts b / a  # Division: Outputs 2
puts b % a  # Modulus: Outputs 0
puts a**2   # Exponent: Outputs 100
  • Comparison Operators: Compare values and return true or false.
a = 10
b = 20

puts a == b  # Equal: Outputs false
puts a != b  # Not equal: Outputs true
puts a > b   # Greater than: Outputs false
puts a < b   # Less than: Outputs true
puts a >= b  # Greater than or equal to: Outputs false
puts a <= b  # Less than or equal to: Outputs true
  • Assignment Operators: Assign values to variables.
a = 10      # Assigns the value 10 to a
a += 5      # Same as a = a + 5, so a is now 15
a -= 5      # Same as a = a - 5, so a is now 10
a *= 2      # Same as a = a * 2, so a is now 20
a /= 2      # Same as a = a / 2, so a is now 10
a %= 3      # Same as a = a % 3, so a is now 1
a **= 2     # Same as a = a ** 2, so a is now 1
  • Logical Operators: Perform logical operations.
a = true
b = false

puts a and b  # Logical AND: Outputs false
puts a or b   # Logical OR: Outputs true
puts not a    # Logical NOT: Outputs false
  • Bitwise Operators: Perform actions on bits.
a = 60  # Binary: 0011 1100
b = 13  # Binary: 0000 1101

puts a & b  # Bitwise AND: Outputs 12 (0000 1100)
puts a | b  # Bitwise OR: Outputs 61 (0011 1101)
puts a ^ b  # Bitwise XOR: Outputs 49 (0011 0001)
puts ~a     # Bitwise NOT: Outputs -61 (1100 0011)
puts a << 2 # Left Shift: Outputs 240 (1111 0000)
puts a >> 2 # Right Shift: Outputs 15 (0000 1111)
  • Ternary Operator: A shorthand way of writing an if-else statement.
a = 10
b = 20

puts a > b ? "a is greater than b" : "a is not greater than b"  # Outputs "a is not greater than b"
  • Range Operators: Create ranges of values.
puts (1..10).to_a  # Outputs [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
puts (1...10).to_a # Outputs [1, 2, 3, 4, 5, 6, 7, 8, 9]

These are some of the basic operators available in Ruby. Remember, operators are the building blocks of any programming language, allowing you to perform operations on values and variables. It's important to understand how and when to use each one.

  1. Arithmetic operators in Ruby: Basic mathematical operations in Ruby.

    a = 5
    b = 2
    
    sum = a + b
    difference = a - b
    product = a * b
    quotient = a / b
    modulus = a % b
    
  2. Comparison operators in Ruby: Operators used to compare values.

    a = 5
    b = 2
    
    equal = a == b
    not_equal = a != b
    greater_than = a > b
    less_than = a < b
    greater_equal = a >= b
    less_equal = a <= b
    
  3. Logical operators in Ruby: Operators used for logical operations.

    x = true
    y = false
    
    logical_and = x && y
    logical_or = x || y
    logical_not = !x
    
  4. Bitwise operators in Ruby: Operators for bitwise manipulation.

    a = 5
    b = 3
    
    bitwise_and = a & b
    bitwise_or = a | b
    bitwise_xor = a ^ b
    bitwise_not = ~a
    left_shift = a << 1
    right_shift = a >> 1
    
  5. Assignment operators in Ruby: Operators used for assigning values.

    a = 5
    
    a += 2  # Equivalent to a = a + 2
    a -= 2
    a *= 2
    a /= 2
    a %= 2
    
  6. Ternary operator in Ruby: A concise way for conditional expressions.

    condition ? true_expression : false_expression
    
  7. Ruby spaceship operator (<=>): Used for comparison and sorting.

    result = a <=> b
    
  8. Ruby range operator (.. and ...): Creates a range of values.

    inclusive_range = 1..5  # Includes 5
    exclusive_range = 1...5  # Excludes 5
    
  9. Operator precedence in Ruby: Determines the order in which operators are evaluated.

    result = 2 + 3 * 4  # Multiplication has higher precedence
    
  10. Overloading operators in Ruby: Customize how objects respond to operators.

    class MyClass
      def +(other)
        # Define custom behavior for the + operator
      end
    end
    
  11. Custom operators in Ruby: Define your own operators.

    class MyClass
      def ~@
        # Define custom behavior for the ~@ operator
      end
    end
    
  12. Ruby unary operators: Operators that operate on a single operand.

    a = 5
    
    positive = +a
    negative = -a
    bitwise_not = ~a
    logical_not = !a
    
  13. Ruby special operators (===, <=>, etc.): Special operators with specific use cases.

    result = pattern === value
    comparison_result = a <=> b