Ruby single and double quotes

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

    In Ruby, double quotes are interpolated, meaning the code in #{} is evaluated as Ruby. Single quotes are treated as literals (meaning the code isn't evaluated).

    var = "hello"
    "#{var} world" #=> "hello world"
    '#{var} world' #=> "#{var} world"
    

    For some extra-special magic, Ruby also offers another way to create strings:

    %Q() # behaves like double quotes
    %q() # behaves like single quotes
    

    For example:

    %Q(#{var} world) #=> "hello world"
    %q(#{var} world) #=> "#{var} world"
    
    0 讨论(0)
  • 2020-11-30 15:41

    You should read the Literals section of the official Ruby documentation.

    It is very concise, so you need to read carefully. But it explains the difference between double-quoted and single-quoted strings, and how they are equivalent to %Q/.../ and %q/.../ respectively.

    0 讨论(0)
  • 2020-11-30 15:43

    In the string interpolation concept, the essential difference between using single or double quotes is that double quotes allow for escape sequences while single quotes do not.

    Let's take an example:

    name = "Mike"
    puts "Hello #{name} \n How are you?"
    

    The above ruby code with string interpolation will interpolate the variable called name which is written inside brackets with its original value which is Mike. And it will also print the string How are you? in a separate line since we already placed an escape sequence there.

    Output:

    Hello Mike 
      How are you?
    

    If you do the same with single quotes, it will treat the entire string as a text and it will print as it is including the escape sequence as well.

    name = Mike'
    puts 'Hello #{name} \n How are you'?
    

    Output:

    Hello #{name} \n How are you?
    
    0 讨论(0)
  • 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"
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 16:00

    If you enclose Ruby string in single qoutes, you can't use interpolation. That's how Ruby works.

    0 讨论(0)
提交回复
热议问题