Search code examples
rubysyntaxstring-literals

Why does not this case expression cause a syntax error?


I wrote a case expression like

case x
when "one" "I"
when "two" "II"
end

It does not cause a syntax error, and always returns nil regardless of the value of x. I think it should cause a syntax error, but the reality is not the case. Why?


Solution

  • The white-space separated string literals get concatenated (see this post). That turns your when statements into legitimate potential matches with no action to be performed, which is why you always get nil.