如何在Ruby中注释多行?


当前回答

=begin
comment line 1
comment line 2
=end

确保=beginand=end是该行的第一项(没有空格)

其他回答

=begin
comment line 1
comment line 2
=end

确保=beginand=end是该行的第一项(没有空格)

=begin
My 
multiline
comment
here
=end
=begin
(some code here)
=end

and

# This code
# on multiple lines
# is commented out

都是正确的。第一种注释类型的优点是可编辑性,因为删除的字符更少,所以取消注释更容易。第二类注释的优点是逐行读取代码的可读性,更容易判断某一行已被注释掉。你的电话,但想想谁会来找你,他们阅读和维护起来有多容易。

使用以下任一项:

=begin
This
is
a
comment
block
=end

or

# This
# is
# a
# comment
# block

是rdoc目前唯一支持的两个,我认为这是一个很好的理由只使用这些。

#!/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!"