协慌网

登录 贡献 社区

Ruby 中的多行注释?

如何在 Ruby 中注释多行?

答案

#!/usr/bin/env ruby

=begin
Every body mentioned this way
to have multiline comments.

The =begin and =end must be at the beginning of the line or
it will be a syntax error.
=end

puts "Hello world!"

<<-DOC
Also, you could create a docstring.
which...
DOC

puts "Hello world!"

"..is kinda ugly and creates
a String instance, but I know one guy
with a Smalltalk background, who
does this."

puts "Hello world!"

##
# most
# people
# do
# this


__END__

But all forgot there is another option.
Only at the end of a file, of course.
  • 这就是它的外观(通过屏幕截图)- 否则很难解释以上注释的外观。 点击放大

文字编辑器中的评论

=begin
My 
multiline
comment
here
=end

尽管存在=begin=end ,但是注释的常规且更正确的方法是在每行上使用# 。如果您阅读任何 ruby 库的源代码,您会发现这几乎是在所有情况下进行多行注释的方式。