Comments are notes written in the code that are completely ignored by the interpreter.
Here's a simple example of comments in Ruby. You can read the rest of the tutorial to learn more.
示例
# Display "Programiz" to the screen
puts "Programiz"
# Output: Programiz
Here, # Display "Programiz" to the screen
is a comment. Ruby skips this line while running the code.
Types of Comments in Ruby
There are two ways to add comments to code
#
- Single-line Comments=begin ... =end
- Multiline Comments
单行注释
Any line that starts with #
is a single-line comment. For example,
name = "John Doe"
# Display name on the console
puts name
输出
John Doe
Here, # Display name on the console
is a single-line comment. Ruby completely ignores it when running the code.
Notice that the comment is written on the line just before the code it explains. You can also use single-line comments right beside the code
name = "John Doe"
puts name # Display name on the console
但是,如果注释很长且描述性强,请避免这样使用注释。
Note: To comment a line of code, press Ctrl + /
for Windows and Cmd + /
for Mac. To uncomment, either remove the #
symbol manually or press the same shortcut again — it toggles the comment on and off.
多行注释
You can add multiline comments in two ways
1. Using multiple # symbols.
The most common way to write a multiline comment in Ruby is by using several single-line comments one after another. For example,
# This is a multiline comment
# created using multiple single-line comments
# The code prints "Hello, World!"
puts "Hello, World!"
输出
Hello, World!
2. Using =begin and =end.
Ruby also allows you to write block comments using =begin
and =end
. Everything between these two lines will be treated as a comment.
=begin
This is a block comment.
It can span multiple lines.
Commonly used for documentation or temporarily disabling code.
=end
puts "Hello, World!"
输出
Hello, World!
Note: =begin
and =end
must be placed at the beginning of a line with no indentation and nothing else on the line.
More on Ruby Comments
To use =begin
and =end
for block comments, you must remember two simple rules
- They must be at the very beginning of the line (no spaces before them).
- They must be on their own lines, with nothing else written on the same line.
If not used properly, Ruby will not recognize them as comments and may show errors
=begin
This won't work!
=end
So, whenever you use =begin
and =end
, make sure they're cleanly placed at the start of a line — no extra spaces!
Comments can be helpful if you want to prevent the execution of code that can still be useful in the future. Consider the program below
puts "Hello John Doe!"
puts "Welcome to our Ruby tutorial."
Suppose the line puts "Welcome to our Ruby tutorial."
isn't required right now. But you know you might change your mind in the future.
在这种情况下,你可以简单地将不需要的行转换为注释,而不是从程序中删除它。
puts "Hello John Doe!"
# puts "Welcome to our Ruby tutorial."
然后,您可以在需要时取消注释该代码。
This method is also useful for debugging since you can isolate code that may cause issues without deleting them.
为什么要使用注释?
As a developer, you'll write code and also need to update or review code written by others.
如果您为自己的代码编写注释,将来您(以及您的同事)将更容易理解它。
As a rule of thumb, use comments to explain why you did something rather than how you did something.
Note: Comments shouldn't be used for explaining poorly written code. Your code should always be well-structured and self-explanatory.