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?
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.
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.
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”.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With