欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解读Ruby中注释的使用方法

程序员文章站 2022-08-29 19:11:27
 ruby行内注释的代码在运行时被忽略。单行注释#字符开始,他们从#到行末如下: #!/usr/bin/ruby -w # this is a s...

 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