Shortcut to make case/switch return a value

Learn shortcut to make case/switch return a value with practical examples, diagrams, and best practices. Covers ruby, switch-statement development techniques with visual explanations.

Returning Values from Case/Switch Statements in Ruby

Hero image for Shortcut to make case/switch return a value

Explore various idiomatic Ruby approaches to make case statements return a value, enhancing code readability and conciseness.

In Ruby, the case statement is an expression, meaning it inherently returns a value. This is a powerful feature that can be leveraged to write more concise and functional code compared to traditional switch statements in other languages that often require explicit break statements and variable assignments. This article will demonstrate how to effectively use Ruby's case statement to return values, along with common patterns and best practices.

The Basic case Statement as an Expression

Unlike C-style switch statements, Ruby's case statement evaluates to the value of the last expression executed within the matching when clause. If no when clause matches and an else clause is present, the value of the else clause is returned. If no when matches and no else is present, the case statement returns nil.

def get_day_type(day)
  case day
  when :monday, :tuesday, :wednesday, :thursday, :friday
    "Weekday"
  when :saturday, :sunday
    "Weekend"
  else
    "Invalid Day"
  end
end

puts get_day_type(:monday)    # => "Weekday"
puts get_day_type(:sunday)    # => "Weekend"
puts get_day_type(:holiday)   # => "Invalid Day"

A basic case statement returning a string based on the input.

Using case for Pattern Matching and Object Comparison

The case statement is not limited to simple equality checks. It uses the === (triple equals) operator for comparison, which allows for powerful pattern matching with various Ruby objects like classes, ranges, and regular expressions. This makes it incredibly versatile for dispatching logic based on different types of input.

def describe_input(input)
  case input
  when Integer
    "It's an integer: #{input}"
  when String
    "It's a string: '#{input}'"
  when (1..10)
    "It's a number between 1 and 10: #{input}"
  when /hello/i
    "It contains 'hello': '#{input}'"
  else
    "Unknown type or value"
  end
end

puts describe_input(5)          # => "It's a number between 1 and 10: 5"
puts describe_input("Hello World") # => "It contains 'hello': 'Hello World'"
puts describe_input(15)         # => "It's an integer: 15"
puts describe_input([])         # => "Unknown type or value"

Demonstrating case with different types of when clauses for pattern matching.

flowchart TD
    A[Input Value] --> B{Case Statement}
    B -- Integer --> C["Return 'It's an integer'"]
    B -- String --> D["Return 'It's a string'"]
    B -- (1..10) --> E["Return 'It's a number between 1 and 10'"]
    B -- /hello/i --> F["Return 'It contains 'hello''"]
    B -- Else --> G["Return 'Unknown type or value'"]

Flowchart illustrating the decision logic of a case statement with various when conditions.

Implicit Receiver and case without an Argument

Ruby's case statement can also be used without an explicit argument. In this form, each when clause is evaluated as a boolean expression. The first when clause that evaluates to true will have its body executed, and its result will be returned. This can be useful for more complex conditional logic where the conditions are not directly related to a single variable.

def get_weather_advice(temperature, raining)
  case
  when temperature < 0
    "Wear a heavy coat and gloves."
  when temperature < 15 && raining
    "Take an umbrella and a light jacket."
  when temperature < 25
    "A light jacket might be nice."
  else
    "Enjoy the warm weather!"
  end
end

puts get_weather_advice(-5, false) # => "Wear a heavy coat and gloves."
puts get_weather_advice(10, true)  # => "Take an umbrella and a light jacket."
puts get_weather_advice(20, false) # => "A light jacket might be nice."

Using a case statement without an argument for complex conditional logic.