Example of writing strings in Ruby, and example of ruby strings

Source: Internet
Author: User

Example of writing strings in Ruby, and example of ruby strings

String interpolation is preferred to replace 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
Code apart from the string. Use spaces to fill in string interpolation. It clarifies the interpolation sources except strings.

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

Consider padding string interpolation code with space. It more clearly sets
Code apart from the string.
Consider leaving the string interpolation white. This makes interpolation clearer in strings.

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

Adopts the consistent string literal reference style. There are two popular styles in the Community, both of which are considered very good-
Single quotation marks (option A) and double quotation marks (option B) are used by default ).

(Option A) when you do not need string interpolation or special characters such as \ t, \ n, ',
Use single quotes first.

    # 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 literally contains "or you need to suppress escape characters (escape characters)
Double quotation marks are preferred.

    # bad    name = 'Bozhidar'    # good    name = "Bozhidar"

The second style is more popular in the Ruby community. The string literal of this Guide, no matter what,
Align with the first style.

Do not use? X literal syntax. Since Ruby 1.9, it is basically redundant ,? X will be interpreted as x (a string containing only one character ).

 # bad  char = ?c  # good  char = 'c'

Do not forget to use {} to enclose the instance and global variables of the inserted string.

 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  end  $global = 0  # bad  puts "$global = #$global"  # good  puts "$global = #{$global}"

Do not use Object # to_s during Object interpolation. It will be called automatically.

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

When operating a large String, avoid using String # + as an alternative to String # <. The cascade String block is always faster than the String # +. It creates multiple String objects.

  # good and also fast  html = ''  html << '

When using heredocs for multi-line strings keep in mind the fact
That they preserve leading whitespace. It's a good practice
Employ some margin based on which to trim the excessive whitespace.
Multiple lines in heredocs retain the blank prefix. Therefore, we should plan how to indent it. This is a good
In this way, a certain margin is used to reduce too many gaps.

  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.