Why do '=begin' and '=end' need to be on their own lines?


#1

Link: https://www.codecademy.com/courses/ruby-beginner-en-d1Ylq/2/2?curriculum_id=5059f8619189a5000201fbcb

So I'm wondering why the two commands for multi-line comments need to be on their own lines.

From the tiny but of messing around I found that it seems to behave correctly even if there is other text on the same line, although after the commands, as the commands like I have in my exercise.

Something interesting I just noticed. When I run the exact same code in my exercise, I copy + pasted, the text after '=end' is grayed out leading me to believe the program recognizes the text 'but what happens if you have one on the last line?' as a comment. However, when I pasted that same code in this comment window and looked at the preview window, it seems to end the multi-line comment after '=end'.

I'm going to assume that the interpreter for forum posts on this site is different than the one used in exercises as the text color of some commands is different and it is behaving differently. Side note: probably not a good idea to use different interpreters for forums vs. exercises if you want to use the forums to troubleshoot any issues people might have as you wouldn't be using the same environment to reproduce any errors or results.

So while this is formatted badly, it still ends with my original question. It looks like there are problems having text after '=end' as not all interpreters will react the same way, should I assume the same for placing text after '=begin' as well?

puts"eric"
=begin comment on this line ps. you can too have a comment on the first line!
this one too puts"eric"
sure why nawt 
=end but what happens if you have one on the last line?
puts"eric"

I hope I'm using the term 'interpreter' correctly, as you can guess I just started. Please let me know if I need to fix anything.


#2

=begin and =end are delimiters. Think of them in the same way you would a block.

loop do
    break
end
puts "loop done"

Anything not in the block will not be executed in the loop. Same applies to a multiline comment. As you can see, the last line contains stray text that is not inside the delimited region. The Ruby interpreter ignores the entire line where =end is written, but a reader would be scratching their head. As for =begin being on its own line, that too would be for readability.

The forum software appears to be quite strict as to how it highlights comments. Since the last line contains text that is outside of the delimited comment block, it does not recognize it as needing to be hightlighted.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.