Example of the use of hundreds of semicolons and literal values in Ruby: ruby percent

Source: Internet
Author: User

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.)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.