Ruby 简明教程

Ruby - Comments

注释是 Ruby 代码中在运行时被忽略的注释行。单行注释以 # 字符开头,并且从 # 扩展到行的末尾,如下所示 −

#!/usr/bin/ruby -w
# This is a single line comment.

puts "Hello, Ruby!"

执行后,以上程序产生以下结果 −

Hello, Ruby!

Ruby Multiline Comments

你可以使用 =begin=end 语法对多行进行注释,如下所示 −

#!/usr/bin/ruby -w

puts "Hello, Ruby!"

=begin
This is a multiline comment and con spwan as many lines as you
like. But =begin and =end should come in the first line only.
=end

执行后,以上程序产生以下结果 −

Hello, Ruby!

确保结尾注释足够远离代码,并且它们容易区分。如果一个块中存在多个结尾注释,请对齐它们。例如 −

@counter      # keeps track times page has been hit
@siteCounter  # keeps track of times all pages have been hit