Ruby行內(nèi)注釋的代碼在運(yùn)行時(shí)被忽略。單行注釋#字符開始,他們從#到行末如下:
1
2
3
4
5
|
#!/usr/bin/ruby -w # This is a single line comment. puts "Hello, Ruby!" |
上述程序執(zhí)行時(shí),會(huì)產(chǎn)生以下結(jié)果:
1
|
Hello, Ruby! |
Ruby的多行注釋
可以注釋掉多行使用 =begin 和 =end 語(yǔ)法如下:
1
2
3
4
5
6
7
8
|
#!/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 |
上述程序執(zhí)行時(shí),會(huì)產(chǎn)生以下結(jié)果:
1
|
Hello, Ruby! |
確保后面的注釋是保持足夠的距離的代碼,能使它很容易區(qū)分。如果在一個(gè)塊中存在一個(gè)以上的尾端注釋它們對(duì)齊。例如:
1
2
|
@counter # keeps track times page has been hit @siteCounter # keeps track of times all pages have been hit |