-

注释是Ruby代码中的注释行,在运行时被忽略。单行注释以#个字符开头,它们从#延伸到行尾,如下所示:

#!/usr/bin/ruby -w

# This is a single line comment.

puts "Hello, Ruby!"

执行时,上述程序产生以下结果 -

Hello, Ruby!

Ruby Multiline注释

您可以使用= 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