ruby_Ruby评论
ruby
Ruby评论 (Ruby comments)
Before learning how comments are applied in Ruby? Let us understand what is exactly meant by comments in programming and what makes them so important.
在学习注释如何在Ruby中应用之前? 让我们了解编程中注释的确切含义以及使其变得如此重要的原因。
The main aim of comments is to make the source code easier for the programmers or developers to understand. They are a kind of documentation for the program which is done to remind the readers about the tricky logics which have been applied during the process of writing the source code. They are not generally processed by compilers or interpreters. They are not typed in programming languages; Human-friendly language is preferred to make a comment entry.
注释的主要目的是使源代码更易于程序员或开发人员理解。 它们是程序的一种文档,旨在提醒读者有关在编写源代码的过程中应用的棘手逻辑的信息。 它们通常不由编译器或解释器处理。 它们不是用编程语言输入的; 首选人类友好的语言进行评论输入。
Various purposes are listed behind making a comment entry:
进行注释条目后列出了各种目的:
Accommodates metadata: Comments accommodate metadata of the source code. It tells about the original and version, name of the developer, current owner, etc.
容纳元数据 :注释容纳源代码的元数据。 它说明原始和版本,开发人员的名称,当前所有者等。
Act as a code descriptor: A code description is employed by the programmer to make the readers understand her logic. It tells the summary of the code.
充当代码描述符 :程序员使用代码描述来使读者理解她的逻辑。 它告诉代码摘要。
Helps in debugging: It provides a great help while debugging a block of code. We apply a print statement to see what the output of the particular block or module is so that we don’t have to face logical errors in our program. It becomes very hectic for a programmer to find where the logical issue is happening. After successful debugging, we make the print statement as the comment entry.
帮助调试 :在调试代码块时,它提供了很大的帮助。 我们使用打印语句来查看特定块或模块的输出是什么,这样我们就不必在程序中遇到逻辑错误。 对于程序员来说,找到逻辑问题发生的位置变得非常忙碌。 成功调试之后,我们将打印语句作为注释条目。
Helps in modification: It helps in the future modification of the code by letting the developer know about the module in which the coder should make changes to fulfill the current or required objective.
有助于修改 :通过使开发人员知道编码器应在其中进行更改以实现当前或所需目标的模块,可以为将来的代码修改提供帮助。
Ruby中的评论 (Comments in Ruby)
Writing comments is considered as a good practice. Comments are also helpful if you temporarily want to disable your code as they are not interpreted or compiled. There are two types of comment which you will need while programming a Ruby code:
写评论被认为是一种好习惯。 如果您暂时不想禁用代码,因为它们不会被解释或编译,则注释也很有用。 编程Ruby代码时需要两种注释:
Single line Comment
单行注释
Multi-line Comment
多行注释
1)单行注释 (1) Single line Comment)
You can use single line comment at the end of the line of code or inline to make the line more understandable to the reader. Remember the following two points about the Single-line comments:
您可以在代码行或内联代码的末尾使用单行注释,以使该行更易于读者理解。 请记住有关单行注释的以下两点:
They start with # symbol. It is known as the pound symbol.
它们以#符号开头。 它被称为英镑符号。
As a good practice, we put space between the starting (#) and elements of the comment.
作为一种好习惯,我们在注释的开始( # )和元素之间放置空格。
Syntax:
句法:
# (elements of the comment)
Example:
例:
puts "Enter Roll No"
#local variable 'roll'
roll = gets.chomp
puts "Enter Name"
name = gets.chomp
puts "Enter percentage"
#conversion of string into integer
per = gets.chomp
if (per.to_i > 40)
puts "Congratulations #{
name} Roll no #{roll}! Your percentage exceeds passing criteria"
else
puts "Hi #{
name} Roll no #{roll}.You are fired!"
end
Output
输出量
Enter Roll No
101
Enter Name
Hritik
Enter percentage
88
Congratulations Hritik Roll no 101! Your percentage exceeds passing criteria
In the above example, you can observe that we have two comment entries i.e. “local variable ‘roll’ (telling about the variable) and “Conversion of String into an integer” (through .to_i method).
在上面的示例中,您可以看到我们有两个注释条目,即“本地变量’roll’ (讲述变量)和“将字符串转换为整数” (通过.to_i方法)。
2)多行注释 (2) Multi-line Comment)
Multi-line comments are also known as Block comments. You can make multi-line comments by writing multiple pound # symbols like this:
多行注释也称为块注释。 您可以通过编写多个井号#来进行多行注释,如下所示:
# (first line of the comment)
# (second line of the comment)
# (third line of the comment)
Example:
例:
#puts "Hello World!"
#puts "Cow gives us milk."
#puts "Satyam is a crazy guy."
You can also select and comment all of it by using =begin … =end. But only modern code editors allow this feature.
您也可以使用= begin … = end选择并注释所有内容。 但是,只有现代代码编辑器才允许使用此功能。
Syntax:
句法:
=begin
comment entry 1
comment entry 2
comment entry 3
=end
Example:
例:
roll = gets.chomp
puts "Enter Name"
name = gets.chomp
puts "Enter percentage"
per = gets.chomp
=begin
if (per.to_i > 40)
puts "Congratulations #{name} Roll no #{roll}! Your percentage exceeds passing criteria"
else
puts "Hi #{name} Roll no #{roll}.You are fired!"
end
=end
puts "You are caught!"
Output
输出量
Enter Roll No
101
Enter Name
Hritik
Enter percentage
88
You are caught!
In the above example, the if…end block does not compiler and the last statement is printing as output.
在上面的示例中,if … end块未编译,并且最后一条语句作为输出打印。
翻译自: https://www.includehelp.com/ruby/comments.aspx
ruby
还没有评论,来说两句吧...