-1

I have one beginner question for Ruby. I have been practicing Ruby with Chris Pine book and I see that he sometimes uses ' ' and sometimes " " after puts method.

Now, I have realized that I cant put any variable with # { } if I use ' ' since I have tried that , and also, if I use for example " " I don't have to discount ' with backslash like this \' (and other way around) but other than that I don't know are there any major differences between these to things or in other words should I sometimes use exclusively ' ' and sometimes " " ?

Tnx

SrdjaNo1
  • 755
  • 3
  • 8
  • 18
  • I'd recommend reading "[How To Ask Questions The Smart Way](http://catb.org/esr/faqs/smart-questions.html)". We don't care if you're new, we expect you to do your research, to try, and to show what you tried in a concise, well-asked question. Jon Skeet's "[Writing the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)" and "[How much research effort is expected of Stack Overflow users?](http://meta.stackoverflow.com/questions/261592)" will help you also. – the Tin Man Jun 04 '17 at 18:18
  • 1
    Ok sorry, it was a language barrier. I was trying to search with signs "" and '', forgot it was called single/double quotes. – SrdjaNo1 Jun 04 '17 at 18:36

1 Answers1

0

There are two differences:

  1. String interpolation: Double quotes allow it, single quotes won't.

    For example:

    name = "SrdjaNo1"
    
    puts "Hi #{name}!"
    #=> Hi SrdjaNo1!
    
    puts 'Hi #{name}!'
    #=> Hi #{name}!
    
  2. Escaping sequences: Single quotes will print them as text.

    For example:

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

Other than that, you could use any of them, just be consistent with its use throughout your code.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Gerry
  • 10,337
  • 3
  • 31
  • 40