BeautifyRuby icon indicating copy to clipboard operation
BeautifyRuby copied to clipboard

Case Statements Not Indenting

Open dustinhansen opened this issue 10 years ago • 1 comments

Is there any way to make case statements indent properly? Most style guides now recommend this and rubocop barks at me that it's not indented properly. Is this possible?

case something
    when this
        do_something
    when that
        do_something_else
end

BeautifyRuby is putting the "when" clauses on the same link as "case".

dustinhansen avatar Jun 16 '15 15:06 dustinhansen

In Ruby Style Guide , which is the style followed by RuboCop, states the following:

  • Indent when as deep as case. This is the style established in both "The Ruby Programming Language" and "Programming Ruby". [link]
# bad
case
  when song.name == 'Misty'
    puts 'Not again!'
  when song.duration > 120
    puts 'Too long!'
  when Time.now.hour > 21
    puts "It's too late"
  else
    song.play
end

# good
case
when song.name == 'Misty'
  puts 'Not again!'
when song.duration > 120
  puts 'Too long!'
when Time.now.hour > 21
  puts "It's too late"
else
  song.play
end

So indenting "when" clauses at the same deep as "case" is the correct way.

pjmartorell avatar Nov 14 '15 18:11 pjmartorell