|
| 1 | +# Case Statement |
| 2 | + |
| 3 | +In Ruby, the `case` statement provides a clean way to express conditional logic when you need to compare a value against multiple conditions. It's similar to if/else chains but often more readable and concise. |
| 4 | + |
| 5 | +```ruby |
| 6 | +# Basic case statement comparing a value against multiple conditions |
| 7 | +grade = "A" |
| 8 | + |
| 9 | +case grade |
| 10 | +when "A" |
| 11 | + puts "Excellent!" |
| 12 | +when "B" |
| 13 | + puts "Good job!" |
| 14 | +when "C" |
| 15 | + puts "Fair" |
| 16 | +else |
| 17 | + puts "Need improvement" |
| 18 | +end |
| 19 | +``` |
| 20 | + |
| 21 | +The `case` statement can also work with ranges, multiple values, and even custom matching using the `===` operator. |
| 22 | + |
| 23 | +```ruby |
| 24 | +# Case statement with ranges and multiple conditions |
| 25 | +score = 85 |
| 26 | + |
| 27 | +case score |
| 28 | +when 90..100 |
| 29 | + puts "A grade" |
| 30 | +when 80..89 |
| 31 | + puts "B grade" |
| 32 | +when 70..79 |
| 33 | + puts "C grade" |
| 34 | +else |
| 35 | + puts "Need improvement" |
| 36 | +end |
| 37 | + |
| 38 | +# Case with multiple values in a single when clause |
| 39 | +day = "Saturday" |
| 40 | + |
| 41 | +case day |
| 42 | +when "Saturday", "Sunday" |
| 43 | + puts "Weekend!" |
| 44 | +else |
| 45 | + puts "Weekday" |
| 46 | +end |
| 47 | +``` |
| 48 | + |
| 49 | +## Pattern Matching (Ruby 2.7+) |
| 50 | + |
| 51 | +Starting from Ruby 2.7, `case` statements support pattern matching, which provides powerful ways to match and destructure data. |
| 52 | + |
| 53 | +```ruby |
| 54 | +# Pattern matching with arrays |
| 55 | +data = [1, 2, 3] |
| 56 | + |
| 57 | +case data |
| 58 | +when [1, 2, 3] |
| 59 | + puts "Exact match!" |
| 60 | +when [1, *rest] |
| 61 | + puts "Starts with 1, followed by #{rest}" |
| 62 | +when Array |
| 63 | + puts "Any array" |
| 64 | +else |
| 65 | + puts "Not an array" |
| 66 | +end |
| 67 | + |
| 68 | +# Pattern matching with hashes (Ruby 3.0+) |
| 69 | +user = { name: "Alice", age: 30 } |
| 70 | + |
| 71 | +case user |
| 72 | +in { name: "Alice", age: } |
| 73 | + puts "Alice is #{age} years old" |
| 74 | +in { name:, age: 20.. } |
| 75 | + puts "#{name} is at least 20" |
| 76 | +else |
| 77 | + puts "No match" |
| 78 | +end |
| 79 | +``` |
| 80 | + |
| 81 | +## Case without an argument |
| 82 | + |
| 83 | +Ruby also allows `case` statements without an explicit argument, which acts like a series of if/elsif conditions. |
| 84 | + |
| 85 | +```ruby |
| 86 | +# Case statement without an argument |
| 87 | +# rubocop:disable Style/EmptyCaseCondition |
| 88 | +case |
| 89 | +when Time.now.saturday? |
| 90 | + puts "It's Saturday!" |
| 91 | +when Time.now.sunday? |
| 92 | + puts "It's Sunday!" |
| 93 | +else |
| 94 | + puts "It's a weekday" |
| 95 | +end |
| 96 | +# rubocop:enable Style/EmptyCaseCondition |
| 97 | +``` |
| 98 | + |
| 99 | +The case statement is particularly useful when you have multiple conditions to check against a single value, or when you want to use pattern matching to destructure complex data structures. |
0 commit comments