* 需要插值與嵌入雙引號的單行字符串使用?`%()`?(是?`%Q`?的簡寫)。多行字符串,最好用 heredocs 。
~~~
# 差(不需要插值)
%(<div class="text">Some text</div>)
# 應該使用 '<div class="text">Some text</div>'
# 差(沒有雙引號)
%(This is #{quality} style)
# 應該使用 "This is #{quality} style"
# 差(多行)
%(<div>\n<span class="big">#{exclamation}</span>\n</div>)
# 應該是一個 heredoc
# 好(需要插值、有雙引號以及單行)
%(<tr><td class="name">#{name}</td>)
~~~
* 沒有?`'`?和?`"`?的字符串不要使用?`%q`。除非需要插值,否則普通字符串可讀性更好。
~~~
# 差
name = %q(Bruce Wayne)
time = %q(8 o'clock)
question = %q("What did you say?")
# 好
name = 'Bruce Wayne'
time = "8 o'clock"
question = '"What did you say?"'
~~~
* 只有正則表達式要匹配多于一個的?`/`?字元時,使用?`%r`。
~~~
# 差
%r{\s+}
# 好
%r{^/(.*)$}
%r{^/blog/2011/(.*)$}
~~~
* 除非調用的命令中用到了反引號(這種情況不常見),否則不要用?`%x`?。
~~~
# 差
date = %x(date)
# 好
date = `date`
echo = %x(echo `date`)
~~~
* 不要用?`%s`?。社區傾向使用?`:"some string"`?來創建含有空白的符號。
* 用?`%`?表示字面量時使用?`()`,?`%r`?除外。因為?`(`?在正則中比較常用。
~~~
# 差
%w[one two three]
%q{"Test's king!", John said.}
# 好
%w(one tho three)
%q("Test's king!", John said.)
~~~