Instructions
Create a function describe_number that uses pattern matching with guards.
Return:
- "zero" for 0
- "positive" for numbers > 0
- "negative" for numbers < 0
Use guard clauses with if:
ruby
case n
in x if x > 0
"positive"
end
Hints:
- Use 'in x if condition' for guards
- The matched value is bound to x
- Handle each case: 0, positive, negative
Your Code
def describe_number(n)
case n
in 0
"zero"
in x if x > 0
"positive"
in x if x < 0
"negative"
end
end
RSpec.describe "describe_number" do
it "returns zero for 0" do
expect(describe_number(0)).to eq("zero")
end
it "returns positive for positive numbers" do
expect(describe_number(5)).to eq("positive")
expect(describe_number(100)).to eq("positive")
end
it "returns negative for negative numbers" do
expect(describe_number(-3)).to eq("negative")
expect(describe_number(-100)).to eq("negative")
end
end
Results
Click "Run Tests" to see results