Ruby single and double quotes

后端 未结 6 998
情话喂你
情话喂你 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:48

    Single-quoted strings don't process escape sequence \ and they don't do string interpolation. For a better understanding, take a look at String concatenation vs. interpolation

    To answer your question, you have to use "" when you want to do string interpolation:

    name = 'world'
    puts "Hello #{name}" # => "Hello world"
    

    Using escape sequence:

    puts 'Hello\nworld'       # => "Hello\nworld"
    puts "Hello\nworld"       # => "Hello
                                    world"
    

提交回复
热议问题