Ruby Keyword: case - rking/pry-docmore GitHub Wiki
A convenient conditional that doesn't require the "subject" to be repeated.
Uses #=== for comparison ("the threequals"), which is defined specially for various objects.
x = 'asdf'
case x
when 'asdf'; puts 'Found it!'
when 'fghj'; puts 'Impossible.'
else puts 'Also impossible.'
end
case x
when String; puts 'Yep, will get here.'
when Array; puts 'Actually, wait a minute.'
when Hash; puts 'This is pretty bad OO.'
else puts 'But, the code works'
end
case x
when /^[fdsa]{4}/; puts 'Whoa, works with regexes.'
else puts 'Will not get here.'
end
Also works with Ranges