Ruby - 注释



注释是在 Ruby 代码中通常被忽略的运行时注解行。单行注释以 # 字符开头,从 # 开始并一直延续到行尾,如下所示 −

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

puts "Hello, Ruby!"

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

Hello, Ruby!

Ruby 多行注释

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