← Ruby EnglishChapter 03 of 13

Operators

## Learning Objectives - Use arithmetic operators - Work with comparison operators - Understand logical operators - Master parallel assignment ## Arithmetic Operators ```ruby a, b = 10, 3 puts a + b # 13 (addition) puts a - b # 7 (subtraction) puts a * b # 30 (multiplication) puts a / b # 3 (integer division) puts a % b # 1 (modulo - remainder) puts a ** b # 1000 (exponentiation) ``` ### Division Behavior ```ruby puts 10 / 3 # 3 (integer division) puts 10.to_f / 3 # 3.333... (float division) puts 10.fdiv(3) # 3.333... (float division) # Integer division truncates toward zero puts -10 / 3 # -3 ``` ### Modulo vs Remainder ```ruby puts 10 % 3 # 1 puts -10 % 3 # 2 (Ruby ensures: a % b has same sign as b) puts 10.modulo(3) # 1 # Use .remainder for truncation toward zero puts -10.remainder(3) # -1 ``` ## Comparison Operators ```ruby puts 5 == 5 # true (equality) puts 5 == "5" # false (type mismatch) puts 5.eql?(5.0) # false (Integer vs Float) puts 5.equal?(5) # true (same object) puts 5 != 3 # true (not equal) puts 5 < 5 # false puts 5 <= 5 # true puts 5 > 3 # true puts 5 >= 5 # true ``` ### Spaceship Operator ```ruby # Returns -1, 0, or 1 puts 5 <=> 5 # 0 (equal) puts 5 <=> 3 # 1 (left is greater) puts 3 <=> 5 # -1 (right is greater) # Commonly used for sorting [3, 1, 2].sort # [1, 2, 3] ``` ### Case Equality Operator ```ruby # Used in case/when statements (1..10) === 5 # true (5 is in range 1-10) /String/ === "Hello" # true (pattern match) # Module/class check String === "hello" # true (hello is a String) ``` ## Logical Operators ```ruby # AND puts true && true # true puts true && false # false puts true and false # true (lower precedence) # OR puts true || false # true puts false || false # false puts false or true # true (lower precedence) # NOT puts !true # false puts not true # false (lower precedence) ``` ### Short-circuit Evaluation ```ruby # && returns first falsy value or last value puts nil && "hello" # nil puts false && "hello" # false puts 5 && "hello" # "hello" # || returns first truthy value or last value puts 5 || nil # 5 puts nil || false # false puts nil || 5 # 5 ``` ### Useful Patterns ```ruby # Set default value name = nil display_name = name || "Anonymous" # Conditional execution is_valid && perform_action # Guard clause def process(data) return unless data # process... end ``` ## Bitwise Operators ```ruby a, b = 5, 3 # Binary: 101 and 011 puts a & b # 1 (AND: 001) puts a | b # 7 (OR: 111) puts a ^ b # 6 (XOR: 110) puts ~a # -6 (NOT) puts a << 1 # 10 (left shift) puts a >> 1 # 2 (right shift) ``` ## Unary Operators ```ruby puts +5 # 5 (positive) puts -5 # -5 (negative) puts --5 # 5 (double negative) puts !!!true # false (not not not) ``` ## Assignment Operators ```ruby x = 10 x += 5 # x = x + 5 = 15 x -= 3 # x = x - 3 = 12 x *= 2 # x = x * 2 = 24 x /= 4 # x = x / 4 = 6 x %= 4 # x = x % 4 = 2 x **= 2 # x = x ** 2 = 36 ``` ## Parallel Assignment ```ruby # Multiple assignment a, b, c = 1, 2, 3 puts "#{a}, #{b}, #{c}" # "1, 2, 3" # Swap without temp variable x, y = 1, 2 x, y = y, x puts "#{x}, #{y}" # "2, 1" # Rest operator head, *tail = [1, 2, 3, 4, 5] puts head # 1 puts tail # [2, 3, 4, 5] # Ignore values first, _, third = [10, 20, 30] puts "#{first}, #{third}" # "10, 30" # Splat operator numbers = [1, 2, 3] puts [*numbers, 4, 5] # [1, 2, 3, 4, 5] ``` ## Chained Assignment ```ruby a = b = c = 0 a += 1 puts "#{a}, #{b}, #{c}" # "1, 0, 0" ``` ## Range Operators ```ruby # Inclusive range (1..5).to_a # [1, 2, 3, 4, 5] # Exclusive range (1...5).to_a # [1, 2, 3, 4] # Used in case statements def grade(score) case score when 90..100 then "A" when 80..89 then "B" when 70..79 then "C" else "F" end end ``` ## Operator Precedence ```ruby # Highest to lowest: # 1. :: (scope resolution) # 2. [] (array/hash access) # 3. ** (exponent) # 4. unary + - # 5. * / % # 6. + - # 7. << >> # 8. & (bitwise AND) # 9. | ^ (bitwise OR/XOR) # 10. > >= < <= # 11. <=> == === != =~ !~ # 12. && (logical AND) # 13. || (logical OR) # 14. .. ... (range) # 15. ? : (ternary) # 16. = += -= etc. # 17. defined? # 18. not # 19. and or (low precedence) # 20. if unless while until ( modifiers) ``` ### Examples ```ruby # Without parentheses puts 2 + 3 * 4 # 14 (not 20) puts (2 + 3) * 4 # 20 # Ternary with logical operators puts true && false ? "yes" : "no" # "no" ``` ## Summary - Arithmetic: `+`, `-`, `*`, `/`, `%`, `**` - Comparison: `==`, `!=`, `<`, `>`, `<=`, `>=`, `<=>`, `===` - Logical: `&&`, `||`, `!`, `and`, `or`, `not` - Assignment: `=`, `+=`, `-=`, `*=`, `/=`, etc. - Spaceship `<=>` returns -1, 0, or 1 - Parallel assignment enables swap without temp variable - Precedence: use parentheses to clarify intent

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →