Rubyで複数行にコメントするにはどうすればよいですか?
10 に答える
#!/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コメントするための通常のより正しい方法は#、各行で'sを使用することです。ルビーライブラリのソースを読むと、ほとんどすべての場合にこれが複数行のコメントが行われる方法であることがわかります。
#!/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!"
次のいずれかを使用します。
=開始 これ は a コメント ブロック =終了
また
# これ # は #a # コメント # ブロック
現在rdocでサポートされているのは2つだけです。これは、これらだけを使用するのに十分な理由だと思います。
=begin
comment line 1
comment line 2
=end
確認=beginし=endて、その行の最初のものです(スペースなし)
=begin
(some code here)
=end
と
# This code
# on multiple lines
# is commented out
どちらも正しいです。最初のタイプのコメントの利点は編集可能性です。削除される文字が少ないため、コメントを外すのが簡単です。2番目のタイプのコメントの利点は、読みやすさです。コードを1行ずつ読み取ると、特定の行がコメント化されていることがはるかに簡単にわかります。あなたの電話ですが、誰があなたの後に来るのか、そして彼らが読んで維持するのがどれほど簡単かを考えてください。
次に例を示します。
=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
誰かが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 %>
これは機能します。
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エンジンは構文の色付けを正しくレンダリングしないことに注意してください。選択したエディターでどのようにレンダリングされるかをテストすることは、演習として行います。;)
