Ruby - 註釋



註釋是在 Ruby 程式碼中通常被忽略的執行時註解行。單行註釋以 # 字元開頭,從 # 開始並一直延續到行尾,如下所示 −

#!/usr/bin/ruby -w
# This is a single line comment.

puts "Hello, Ruby!"

執行上述程式時,將產生以下結果 −

Hello, Ruby!

Ruby 多行註釋

可以使用 =begin=end 語法對多行進行註釋,如下所示 −

#!/usr/bin/ruby -w

puts "Hello, Ruby!"

=begin
This is a multiline comment and con spwan as many lines as you
like. But =begin and =end should come in the first line only. 
=end

執行上述程式時,將產生以下結果 −

Hello, Ruby!

確保尾隨註釋與程式碼有足夠的距離,並且很容易辨別。如果在一個塊中存在多個尾隨註釋,請將它們對齊。例如,−

@counter      # keeps track times page has been hit
@siteCounter  # keeps track of times all pages have been hit
廣告
© . All rights reserved.