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

Style / HashLikeCase added to rubocop

The rule is to define it as an array if it is not necessary to describe it in the case statement.

Added with rubocop 0.88.0, it's 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 history

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

PR and implementation

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

Recommended Posts

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