Ruby single and double quotes

后端 未结 6 996
情话喂你
情话喂你 2020-11-30 15:01

I\'ve recently been coding in Ruby and have come from Python, where single and double quotes made no difference to how the code worked as far as I know.

I moved to R

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 15:55

    Ruby supports single-quoted string, for many uses like as follow:

    >> 'foo'
    => "foo"
    >> 'foo' + 'bar'
    => "foobar"
    

    In above example, those two types of strings are identical. We can use double quote in place of single quote and we will get same output like above example.

    As you face problem, while using interpolation in single quoted string because Ruby do not interpolate into single-quoted string. I am taking one example for more understanding:

    >> '#{foo} bar'
    => "\#{foo} bar"
    

    Here you can see that return values using double-quoted strings, which requires backslash to escape special characters such as #.

    Single quoted string often useful because they are truly literal.

提交回复
热议问题