When uses the === operator to compare the value given to case against the argument given to when. Also 'then' is unnecessary when appearing on a different line from the when statement The correct code for what you're trying to do is:
case x
when 16
puts 'hi'
when Object
puts 'obj'
end
As for the new addition to the question.
case user.roles.included? (... magic ...)
when ['admin', 'editor']
...
when ['anonymous']
...
end
Doesn't work because Array's === doesn't map to include. I'm not sure where Array's === operator comes from or even what it does. But you could override it to provide the functionality you want.
Judging by the above code you want the case to trigger if one of the users roles matches the array. This will override Array#=== to do just that.
class Array
def === other_array
! (other_array & self).empty?
end
end
case user.roles
when ['admin', 'editor']
...
when ['anonymous']
...
end
Caveat: Depending on where you override Array#=== this can have unforeseen consequences. As it will change all arrays in that scope. Given that === is being inherited from Object where it is an alias for ==, I'm not expecting it to be a big problem.
Places where the new === differs from the old ===:
- new === will return true if either array is a subset or reordering of the other.
- old === will only return true if the two arrays are identical (order and contents)
So far as I know, case/when is the only time === could be implicitly called on an array.