Example of the use of hundreds of semicolons and literal values in Ruby: ruby percent
Use % () (abbreviated as % Q) for a single line string that requires interpolation and embedding double quotation marks ). Multi-line string, preferably heredocs.
# bad (no interpolation needed) %(<div class="text">Some text</div>) # should be '<div class="text">Some text</div>' # bad (no double-quotes) %(This is #{quality} style) # should be "This is #{quality} style" # bad (multiple lines) %(<div>\n<span class="big">#{exclamation}</span>\n</div>) # should be a heredoc. # good (requires interpolation, has quotes, single line) %(<tr><td class="name">#{name}</td>)
Do not use % q for strings without 'and. Unless many characters need to be escaped, the normal string is more readable.
# bad name = %q(Bruce Wayne) time = %q(8 o'clock) question = %q("What did you say?") # good name = 'Bruce Wayne' time = "8 o'clock" question = '"What did you say?"'
The % r method is only applicable to defining regular expressions that contain multiple symbols.
# bad %r(\s+) # still bad %r(^/(.*)$) # should be /^\/(.*)$/ # good %r(^/blog/2011/(.*)$)
% X is not used unless the reverse quotation marks are used in the called command (this is not common.
# bad date = %x(date) # good date = `date` echo = %x(echo `date`)
Do not use % s. Community tends to use: "some string" to create symbols containing white spaces.
() Is used to represent the literal volume with %, except for % r. Because braces often appear in regular expressions in many scenarios, for example, {as a separator may be a better choice, depending on the content of the regular expression.
# bad %w[one two three] %q{"Test's king!", John said.} # good %w(one two three) %q("Test's king!", John said.)