Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Ruby on Rails generate add a blank line to the end of a file?

Running rails generate controller Foo home results in:

class FooController < Application Controller
  def home
  end
end
(There's nothing on this line in the actual file, just a blank line)

Is there a purpose to this blank line?

like image 718
Neil Kelty Avatar asked Apr 18 '13 01:04

Neil Kelty


People also ask

Why is there an empty line at the end of a file?

The empty line in the end of file appears so that standard reading from the input stream will know when to terminate the read, usually returns EOF to indicate that you have reached the end. The majority of languages can handle the EOF marker.

Why should you add new line at the end of the file?

If the last line in a file doesn't end with a newline then addition of next line affects two lines instead of one. This also pollutes diff on multiple files, so reader may wonder what has changed in a line whereas no significant change has occured.

Should all files have a new line at the end?

So, it turns out that, according to POSIX, every text file (including Ruby and JavaScript source files) should end with a \n , or “newline” (not “a new line”) character. This acts as the eol , or the “end of line” character. It is a line “terminator”.


2 Answers

It's common to use a newline to indicate end of line, even for the last line. Many editors (like vi, which I use) add in this newline silently. The consensus is that text files (especially in the Unix world) should end in a newline, and there have been problems historically if it wasn't present. Why should text files end with a newline?

The tool I used to count the lines in a file "wc" just counts the newlines in a file, so without that trailing newline it would show 3 instead of 4.

It also improves the readability of the templates used in the generator. Consider: https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/controller/templates/controller.rb

To remove the trailing newline, that template would have the last line of:

end<% end -%>

instead of:

end
<% end -%>

That seems less readable to me.


The discussion below refers to a blank line earlier in the file, instead of the trailing newline character.

It was an oversight and fixed in a later version of rails.

You can see in the commit history here where the blank lines get removed:

https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/controller/templates/controller.rb

This is the commit where it was removed:
Remove redundant blank line at the bottom

It also shows you why it was there, previously they were just adding a blank line after each action.

like image 80
Shawn Balestracci Avatar answered Oct 22 '22 02:10

Shawn Balestracci


It is a common practice to end a file with a newline when keeping your source code in SCM such as git. Suppose, you add something at the end and commit the changes. Now compare the diffs in two cases.

1) Ending with newline:

--- foo_controller.rb   2013-04-18 09:14:48.000000000 +0800
+++ foo_controller2.rb  2013-04-18 09:15:10.000000000 +0800
@@ -1,4 +1,7 @@
 class FooController < ApplicationController
   def home
   end
-end
\ No newline at end of file
+end
+
+p FooController.methods
\ No newline at end of file

2) No newline:

--- foo_controller.rb   2013-04-18 09:16:28.000000000 +0800
+++ foo_controller2.rb  2013-04-18 09:16:35.000000000 +0800
@@ -2,3 +2,5 @@
   def home
   end
 end
+
+p FooController.methods

You see that diff treats "end" and "end\n" as two different lines which causes less clean view in the first case.

like image 42
Simon Perepelitsa Avatar answered Oct 22 '22 00:10

Simon Perepelitsa