[Ruby] Style/HashLikeCase Consider replacing case-when with a hash lookup added to rubocop

less than 1 minute read

Style/HashLikeCase added to rubocop

If you don’t need to describe it in case statement, let’s define it as an array.

Added in rubocop 0.88.0, very good.

Bad example


type ='test'
case type
when'interview'
   'interview'
when'test'
   'test'
when'standard'
   'Normal'
end

good example


type ='test'
lists = {
   'interview' =>'interview',
   'test' =>'test',
   'standard' =>'normal',
}
lists[type]

Official repository information

issue and background

https://github.com/rubocop-hq/rubocop/issues/8247

PR and implementation

https://github.com/rubocop-hq/rubocop/pull/8280