control_flow/case_variable_binding.rb
tests/_examples/control_flow/case_variable_binding.rb in the metorex repository.
# Case statements with variables
# Simple variable
value = 42
x = value
puts "Matched: #{x}"
# Multiple cases
status = 404
case status
when 200
puts "OK"
when 404
puts "Not Found"
when 500
puts "Internal Server Error"
else
puts "Status code: #{status}"
end
# Conditional logic
age = 25
if age >= 18
if age < 65
puts "Working age: #{age}"
else
puts "Retirement age: #{age}"
end
elsif age < 18
puts "Minor: #{age}"
else
puts "Retirement age: #{age}"
end
Output:
Matched: 42
Not Found
Working age: 25