Pattern Matching • Exercise 4/4

Pattern Match with Types

00:00
0
30 points

Instructions

Create a function describe_value that describes different types of values
using pattern matching.

Return:
- "empty array" for []
- "single: X" for array with one element (where X is the element)
- "pair: X, Y" for array with exactly two elements
- "many: N elements" for arrays with more than 2 elements
- "number: X" for integers
- "text: X" for strings
- "unknown" for anything else

Use type checking in patterns:
ruby
case value
in Integer => n
"number: #{n}"
in [a, b]
"pair"
end

Your Code

Results

Click "Run Tests" to see results