String Writing in Ruby sample _ruby topic

Source: Internet
Author: User

Use string interpolation instead of string concatenation.

  # bad
  email_with_name = user.name + ' < ' + User.email + ' > '

  # good
  email_with_name = ' #{user.name} <# {user.email}> "

  # good
  email_with_name = Format ('%s <%s> ', User.Name, User.email)

Consider padding string interpolation code with space. It more clearly sets the
The code apart from the string. Consider using a space to fill strings interpolation. It is more explicit in addition to the string interpolation source.

  "#{User.last_name}, #{user.first_name}"

Consider padding string interpolation code with space. It more clearly sets the
The code apart from the string.
Consider leaving white for string interpolation. This makes interpolation more clear in the string.

  "#{User.last_name}, #{user.first_name}"

Takes a consistent string literal reference style. There are two popular styles in the community that are considered very good-
The default is to use single quotes (option A) and double quote style (option B).

(Option A) When you do not need string interpolation or special symbols such as \ \ \ n, '
The use of single quotation mark reference is preferred.

    # bad
    name = "Bozhidar"

    # good
    name = ' Bozhidar '

(Option B) Prefer double-quotes unless your string literal
Contains "or escape characters you want to suppress.
Unless your string literal contains "or you need to suppress the escape character" (Escape characters)
Use double quotation marks as a preferred reference.

    # bad
    name = ' Bozhidar '

    # good
    name = ' Bozhidar '

The second style can be said to be more popular in the Ruby community. The literal amount of the guide's string, Anyhow,
Aligned with the first style.

Do not use the X symbol literal syntax. Starting with Ruby 1.9, it's basically redundant, and X will be interpreted as X (a string that contains only one character).

 # bad
  char =? C

  # good
  char = ' C '

Don't forget to use {} to surround the instance of the inserted string with the global variable.

 Class Person
   attr_reader:first_name,: last_name

   def initialize (first_name, last_name)
    @first_name = first_name
    @last_name = last_name
   End

   # bad-valid, but awkward
   def to_s
    "# @first_name # @last_ Name '
   End

   # good
   def to_s
    ' #{@first_name} #{@last_name} '
   end

  $global = 0
  # Bad
  puts "$global = # $global"

  # good
  puts "$global = #{$global}"

Do not use object#to_s when the object is interpolated, it will be invoked automatically.

  # bad message = ' This is the
  #{result.to_s}. '

  # Good message = ' This is the
  #{result}. '

When manipulating larger strings, avoid using string#+ as an alternative to using string#<<. In-place cascading string blocks are always faster than string#+, which creates multiple string objects.

  # Good and also fast
  HTML = '
  HTML << '  
 

When using Heredocs to multi-line strings keep in mind the fact
that they preserve leading whitespace. It ' s a good practice to
Employ some margin based on which to trim the excessive whitespace.
Multiple lines of text in Heredocs retain prefix whitespace. So do the planning of how to indent. This is a good
Practices, the use of a certain graphic on this basis to cut too much blank.

  Code = <<-end.gsub (/^\s+\|/, ')
   |def Test
   | some_method | other_method |end end
  #= > "def test\n some_method\n other_method\nend\n"


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.