Percent Literals
Prefer parentheses over curly
braces, brackets, or pipes when using%
-literal delimiters for
consistency, and because the behavior of%
-literals is closer to method
calls than the alternatives.[link]# bad
%w[date locale]
%w{date locale}
%w|date locale|
# good
%w(date locale)
Use
%w
freely.[link]STATES = %w(draft open closed)
Use
%()
for single-line strings which require
both interpolation and embedded double-quotes. For multi-line strings,
prefer heredocs.[link]# bad - no interpolation needed
%(Welcome, Jane!)
# should be 'Welcome, Jane!'
# bad - no double-quotes
%(This is #{quality} style)
# should be "This is #{quality} style"
# bad - multiple lines
%(Welcome, Jane!\nPlease enjoy your stay at #{location}\nCheers!)
# should be a heredoc.
# good - requires interpolation, has quotes, single line
%(Welcome, #{name}!)
Use
%r
only for regular expressions matching more
than one ‘/‘ character.[link]# bad
%r(\s+)
# still bad
%r(^/(.*)$)
# should be /^\/(.*)$/
# good
%r(^/blog/2011/(.*)$)
Avoid the use of %x unless you’re going to invoke a
command with backquotes in it (which is rather unlikely).
[link]# bad
date = %x(date)
# good
date = `date`
echo = %x(echo `date`)