Question

Is there a best practice in ruby coding style for the indentation of blank lines? For example, if I have a blank line between two method definitions within a class, should the blank line be indented to the same level as the method definitions, or should it be truly blank?

class Foo
  def bar
  end

  def baz
  end
end

For a specific example, in the code above, should the line between the end of bar's definition and def baz lines contain two spaces like the lines above and below it and a newline, or simply a newline?

I've tried googling this question, searching SO, and looking at style guides, but coming up empty. Maybe I'm phrasing it oddly? Does it not matter? Are there differing opinions? Is there a standard within Rails specifically that differs from the broader Ruby community?

Was it helpful?

Solution

Just about any style guide you look at will tell you to "avoid trailing whitespace", where trailing whitespace is any non-newline whitespace character on a line that is not followed by a non-whitespace character on the same line.

See https://github.com/bbatsov/ruby-style-guide for example, or github's internal ruby style guide.

Since a blank line has no non-whitespace characters, any spaces or tabs on a blank line would constitute trailing whitespace, and should be avoided.

OTHER TIPS

There's no point in putting whitespace on a line where there's no code and most editors and IDEs are not going to help you do this either. Have a look at any of the Rails or Ruby source code, which does not do this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top