11

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?

0

2 Answers 2

13

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.

6
  • Can you point to the specific commit? There are a few newline related commits, but none that I can see about the file ending. Apr 18, 2013 at 5:04
  • This commit removes newline after the final action. There is still one at the end of the file. Apr 18, 2013 at 5:20
  • What version of rails are you using? I tried several and none had a trailing newline. Apr 18, 2013 at 5:35
  • The latest Rails 3.2.13. Are you sure we are talking about the same newline? I mean the newline right at the end of a file. (See snippet in the question above.) Apr 18, 2013 at 5:42
  • Hmm, There's no newline when I generate a controller in 3.2.13 wc ap/controllers/foo_controller.rb 4 8 65 app/controllers/foo_controller.rb Apr 18, 2013 at 5:47
6

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.