Ruby中的字串編寫樣本_ruby專題

來源:互聯網
上載者:User

優先使用 字串插值 來代替 字串串聯。

  # 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
    code apart from the string.考慮使用空格填充字串插值。它更明確了除字串的插值來源。

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

    Consider padding string interpolation code with space. It more clearly sets the
    code apart from the string.
    考慮替字串插值留白。這使插值在字串裡看起來更清楚。

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

    採用一致的字串字面量引用風格。這裡有在社區裡面受歡迎的兩種風格,它們都被認為非常好 -
    預設使用單引號(選項 A)以及雙引號風格(選項 B)。

        (Option A) 當你不需要字串插值或者例如 \t, \n, ' 這樣的特殊符號的
        時候優先使用單引號引用。

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

        (Option B) Prefer double-quotes unless your string literal
        contains " or escape characters you want to suppress.
        除非你的字串字面量包含 " 或者你需要抑制逸出字元(escape characters)
        優先使用雙引號引用。

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

    第二種風格可以說在 Ruby 社區更受歡迎些。該指南的字串字面量,無論如何,
    與第一種風格對齊。

    不要使用 ?x 符號字面量文法。從 Ruby 1.9 開始基本上它是多餘的,?x 將會被解釋為 x (只包括一個字元的字串)。

  

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

    別忘了使用 {} 來圍繞被插入字串的執行個體與全域變數。

  

 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}"

    在對象插值的時候不要使用 Object#to_s,它將會被自動調用。

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

    操作較大的字串時, 避免使用 String#+ 做為替代使用 String#<<。就地級聯字串塊總是比 String#+ 更快,它建立了多個字串對象。

  # good and also fast  html = ''  html << '<h1>Page title</h1>'  paragraphs.each do |paragraph|   html << "<p>#{paragraph}</p>"  end

    When using heredocs for 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.
    heredocs 中的多行文字會保留首碼空白。因此做好如何縮排的規劃。這是一個很好的
    做法,採用一定的邊幅在此基礎上削減過多的空白。

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.