簡體   English   中英

Ruby 中的多行注釋?

[英]Multi-Line Comments in 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​​ 庫的源代碼,您會發現在幾乎所有情況下,這都是多行注釋的處理方式。

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

使用:

=begin
This
is
a
comment
block
=end

或者

# This
# is
# a
# comment
# block

是 rdoc 當前唯一支持的兩個,這是我認為僅使用這些的一個很好的理由。

=begin
(some code here)
=end

# This code
# on multiple lines
# is commented out

都是正確的。 第一種注釋的優點是可編輯性——取消注釋更容易,因為刪除的字符較少。 第二種注釋的優點是可讀性——逐行閱讀代碼,更容易看出某行已被注釋掉。 您的電話,但請考慮誰會跟隨您以及他們閱讀和維護的難易程度。

這是一個例子:

=begin 
print "Give me a number:"
number = gets.chomp.to_f

total = number * 10
puts  "The total value is : #{total}"

=end

您放置在=begin=end之間的所有內容都將被視為注釋,無論它包含多少行代碼。

注意:確保=begin之間沒有空格:

  • 正確: =begin
  • 錯誤: = begin
=begin
comment line 1
comment line 2
=end

確保=begin=end是該行的第一件事(沒有空格)

如果有人正在尋找一種在 Ruby on Rails 中對 html 模板中的多行進行注釋的方法,則 =begin =end 可能存在問題,例如:

<%
=begin
%>
  ... multiple HTML lines to comment out
  <%= image_tag("image.jpg") %>
<%
=end
%>

會因為 %> 關閉 image_tag 而失敗。

在這種情況下,這是否是注釋掉可能是有爭議的,但我更喜歡用“if false”塊將不需要的部分括起來:

<% if false %>
  ... multiple HTML lines to comment out
  <%= image_tag("image.jpg") %>
<% end %>

這將起作用。

  def idle
    <<~aid
    This is some description of what idle does.

    It does nothing actually, it's just here to show an example of multiline
    documentation. Thus said, this is something that is more common in the
    python community. That's an important point as it's good to also fit the
    expectation of your community of work. Now, if you agree with your team to
    go with a solution like this one for documenting your own base code, that's
    fine: just discuss about it with them first.

    Depending on your editor configuration, it won't be colored like a comment,
    like those starting with a "#". But as any keyword can be used for wrapping
    an heredoc, it is easy to spot anyway. One could even come with separated
    words for different puposes, so selective extraction for different types of
    documentation generation would be more practical. Depending on your editor,
    you possibly could configure it to use the same syntax highlight used for
    monoline comment when the keyword is one like aid or whatever you like.

    Also note that the squiggly-heredoc, using "~", allow to position
    the closing term with a level of indentation. That avoids to break the visual reading flow, unlike this far too long line.
    aid
  end

請注意,在發布時,stackoverflow 引擎無法正確呈現語法着色。 測試它在您選擇的編輯器中的呈現方式作為練習。 ;)

我不確定它的工作方式,但是您可以在要注釋的代碼的開頭和結尾加上斜杠

puts "hello"
/
puts "world"
/
puts "peace"

輸出值

hello
peace

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM