ruby注释_在Ruby中使用注释

ruby注释

Comments in your Ruby code are notes and annotations meant to be read by other programmers. The comments themselves are ignored by the Ruby interpreter, so the text inside the comments isn't subject to any restrictions.

Ruby代码中的注释是注解和注解,旨在供其他程序员阅读。 注释本身被Ruby解释器忽略,因此注释内的文本不受任何限制。

It's usually good form to put comments before classes and methods as well any piece of code that may be complex or unclear.

通常,将注释放在类和方法以及任何可能复杂或不清楚的代码之前是一种很好的形式。

有效使用评论 ( Using Comments Effectively )

Comments should be used to give background information or annotate difficult code. Notes that simply say what the next line of straightforward code does are not only obvious but also add clutter to the file.

注释应用于提供背景信息或注释困难的代码。 只需简单说明下一行简单代码的注释,不仅显而易见,而且还会使文件混乱。

It's important to take care not to use too many comments and to be sure the comments made in the file are meaningful and helpful to other programmers.

重要的是要注意不要使用过多的注释,并确保在文件中所做的注释有意义并且对其他程序员有帮助。

舍邦 ( The Shebang )

You'll notice that all Ruby programs start with a comment that begins with #!. This is called a shebang and is used on Linux, Unix and OS X systems.

您会注意到,所有Ruby程序都以开头的注释开头 。 这称为Shebang ,用于Linux,Unix和OS X系统。

When you execute a Ruby script, the shell (such as bash on Linux or OS X) will look for a shebang at the first line of the file. The shell will then use the shebang to find the Ruby interpreter and run the script.

当您执行Ruby脚本时,外壳程序(例如Linux或OS X上的bash)将在文件的第一行查找shebang。 然后,shell将使用shebang查找Ruby解释器并运行脚本。

The preferred Ruby shebang is #!/usr/bin/env ruby, though you may also see #!/usr/bin/ruby or #!/usr/local/bin/ruby.

首选的Ruby shebang是#!/ usr / bin / env ruby ,尽管您可能还会看到#!/ usr / bin / ruby#!/ usr / local / bin / ruby

单行注释 ( Single-Line Comments )

The Ruby single-line comment begins with the # character and ends at the end of the line. Any characters from the # character to the end of the line are completely ignored by the Ruby interpreter.

Ruby单行注释以字符开头,并在该行的结尾处结束。 从字符到行尾的所有字符都将被Ruby解释器完全忽略。

The # character doesn't necessarily have to occur at the beginning of the line; it can occur anywhere.

字符不一定必须出现在行的开头; 它可以发生在任何地方。

The following example illustrates a few uses of comments.

以下示例说明了注释的几种用法。

 #!/usr/bin/env ruby
 # This line is ignored by the Ruby interpreter
 # This method prints the sum of its arguments
 def sum(a,b)
   puts a+b
 end
 sum(10,20) # Print the sum of 10 and 20 

多行注释 ( Multi-Line Comments )

Though often forgotten by many Ruby programmers, Ruby does have multi-line comments. A multi-line comment begins with the =begin token and ends with the =end token.

尽管许多Ruby程序员经常忘记它,但Ruby确实有多行注释。 多行注释以= begin标记开始 ,以= end标记结束

These tokens should start at the beginning of the line and be the only thing on the line. Anything between these two tokens is ignored by the Ruby interpreter.

这些标记应从行的开头开始,并且是行上唯一的东西。 这两个标记之间的任何内容都会被Ruby解释器忽略。

 #!/usr/bin/env ruby
 =begin
 Between =begin and =end, any number
 of lines may be written. All of these
 lines are ignored by the Ruby interpreter.
 =end
 puts "Hello world!"

In this example, the code would execute as Hello world!

在此示例中,代码将以Hello world的身份执行

翻译自: https://www.thoughtco.com/commenting-ruby-code-2908193

ruby注释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值