814

Rubyで複数行にコメントするにはどうすればよいですか?

4

10 に答える 10

1446
#!/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.
  • これは(スクリーンショットを介して)どのように見えるかです-そうでなければ、上記のコメントがどのように見えるかを解釈するのは難しいです。クリックしてズームイン

テキストエディタのコメント

于 2010-06-07T16:25:40.457 に答える
136
=begin
My 
multiline
comment
here
=end
于 2010-06-07T13:12:31.873 に答える
62

=beginとの存在にもかかわらず、=endコメントするための通常のより正しい方法は#、各行で'sを使用することです。ルビーライブラリのソースを読むと、ほとんどすべての場合にこれが複数行のコメントが行われる方法であることがわかります。

于 2010-06-08T18:24:08.007 に答える
22
#!/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!"
于 2010-06-07T13:12:57.183 に答える
18

次のいずれかを使用します。

=開始
これ
は
a
コメント
ブロック
=終了

また

# これ
# は
#a
# コメント
# ブロック

現在rdocでサポートされているのは2つだけです。これは、これらだけを使用するのに十分な理由だと思います。

于 2014-08-05T19:52:21.447 に答える
15
=begin
comment line 1
comment line 2
=end

確認=begin=endて、その行の最初のものです(スペースなし)

于 2018-08-28T15:03:34.197 に答える
14
=begin
(some code here)
=end

# This code
# on multiple lines
# is commented out

どちらも正しいです。最初のタイプのコメントの利点は編集可能性です。削除される文字が少ないため、コメントを外すのが簡単です。2番目のタイプのコメントの利点は、読みやすさです。コードを1行ずつ読み取ると、特定の行がコメント化されていることがはるかに簡単にわかります。あなたの電話ですが、誰があなたの後に来るのか、そして彼らが読んで維持するのがどれほど簡単かを考えてください。

于 2014-04-21T03:08:18.473 に答える
14

次に例を示します。

=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
于 2014-08-15T16:02:29.627 に答える
3

誰かがRubyonRailsのhtmlテンプレートの複数行にコメントする方法を探している場合、たとえば次のように= begin=endに問題がある可能性があります。

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

%>image_tagを閉じるために失敗します。

この場合、これがコメントアウトされているかどうかは議論の余地がありますが、不要なセクションを「iffalse」ブロックで囲むことをお勧めします。

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

これは機能します。

于 2018-06-17T21:59:29.217 に答える
2
  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エンジンは構文の色付けを正しくレンダリングしないことに注意してください。選択したエディターでどのようにレンダリングされるかをテストすることは、演習として行います。;)

于 2020-04-26T03:57:30.440 に答える