[Ruby] Quote(') and double quote(") explained

Post any examples or modules that you want to share here
Post Reply
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

[Ruby] Quote(') and double quote(") explained

Post by tulamide »

Even as a beginner, you probably saw that you can use quotes or double quotes to assign a string:

Code: Select all

mystring = "Hello World"
mystring = 'Hello World'


That doesn't mean they're redundant. But their behaviour only differs in two situations. The first one happens, when the text you want to assign as a string contains a double quote or quote. You then need to use the other one to build a string, or else you get an error:

Code: Select all

mystring = "Hello"World" #produces an error
mystring = 'Hello"World' #works as expected
mystring = 'Hello'World' #produces an error
mystring = "Hello'World" #works as expected


The other situations are escaped control signs. The ascii set contains control signs like new line, tab, return and others. Those can be used in strings to format them:

Code: Select all

mystring = "Hello\nWorld" #will use seperate lines for Hello and World ( \n instructs a newline)


With single quotes, the characters are not interpreted, but taken as they are:

Code: Select all

mystring = 'Hello\nWorld' #will just print Hello\nWorld, no new line is created.


This is very handy, when you consider Windows. Say, you have a folder named music, containing a folder named rave, which contains a folder named new and lastly it contains a folder named tulamide. See what happens if you use double quotes:

Code: Select all

mystring = "C:\music\rave\new\tulamide"
##results in:
C:musicave
ewulamide


Two lines of nonsense, that Windows will complain about. With single quotes it ends exactly as you need it:

Code: Select all

mystring = 'C:\music\rave\new\tulamide'
##results in:
C:\music\rave\new\tulamide


Very comfortable! But to add to your confusion, there's a way to get to the correct result with double quotes as well. And if I wouldn't mention it, chances are the gurus will chop my head off.
You can escape from the escape! This is done by adding a \ to every escaped control char:

Code: Select all

C:\\music\\rave\\new\\tulamide
##results in:
C:\music\rave\new\tulamide


But this the uncomfortable way, so I recommend single quotes instead.
"There lies the dog buried" (German saying translated literally)
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Re: [Ruby] Quote(') and double quote(") explained

Post by Spogg »

I love these mini tutorials you do!

I do hope there'll be more to come.

Cheers

Spogg
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: [Ruby] Quote(') and double quote(") explained

Post by RJHollins »

If I may QUOTE Spogg .... love em. Thanks T.
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: [Ruby] Quote(') and double quote(") explained

Post by KG_is_back »

There is also another very important difference. Double quote strings allow string interpolation using special #{} command, that allows you to insert data into string:

Code: Select all

h=182
m=67.684

wh="Height: #{ h.to_s}cm Weight: #{m.round(1).to_s}kg" #-> "Height: 182cm Weight: 67.7kg"
bmi="Body Mass Index: #{ (10000*m/(h*h)).round(3).to_s }" #-> "Body Mass Index: 20.434"


You can get identical result by appending strings, but that's slower and slightly harder to read:

Code: Select all

h=182
m=67.684

wh="Height: "+ h.to_s+"cm Weight: "+m.round(1).to_s+"kg" #-> "Height: 182cm Weight: 67.7kg"
bmi="Body Mass Index: "+ (10000*m/(h*h)).round(3).to_s #-> "Body Mass Index: 20.434"
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: [Ruby] Quote(') and double quote(") explained

Post by tulamide »

@Spogg & RJ
Whenever I spot something, that I think is not too complicated, I will do another post. Thank you!

@KG
Good point! I too use #{} a lot nowadays. In the beginning I did it the hard way of adding strings, just as I was used to from older languages. But #{} is such a very nice shortcut, whose functionality is present in other modern languages as well, so I guess it was inspired by them. Just a little correction: You don't need ::to_s. It is called automatically when the variable doesn't represent a string. "... #{h} cm ..." is sufficient.

Yes, the use of #{} is only possible in strings with double quotes, guys!
"There lies the dog buried" (German saying translated literally)
Post Reply