Triple single quote vs triple double quote in Ruby

后端 未结 4 1125
天涯浪人
天涯浪人 2021-01-06 08:13

Why might you use \'\'\' instead of \"\"\", as in Learn Ruby the Hard Way, Chapter 10 Study Drills?

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-06 08:38

    There are no triple quotes in Ruby.

    Two String literals which are juxtaposed are parsed as a single String literal. So,

    'Hello' 'World'
    #=> "HelloWorld"
    

    is the same as

    'HelloWorld'
    #=> "HelloWorld"
    

    And

    '' 'Hello' ''
    #=> "Hello"
    

    is the same as

    '''Hello'''
    #=> "Hello"
    

    is the same as

    'Hello'
    #=> "Hello"
    

    Since adding an empty string literal does not change the result, you can add as many empty strings as you want:

    """"""""""""'''''Hello'''''''''
    #=> "Hello"
    

    There are no special rules for triple single quotes vs. triple double quotes, because there are no triple quotes. The rules are simply the same as for quotes.

提交回复
热议问题