Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

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

Post any examples or modules that you want to share here

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

Postby tulamide » Sun Apr 22, 2018 12:10 pm

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)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

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

Postby Spogg » Sun Apr 22, 2018 12:22 pm

I love these mini tutorials you do!

I do hope there'll be more to come.

Cheers

Spogg
User avatar
Spogg
 
Posts: 3358
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England

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

Postby RJHollins » Sun Apr 22, 2018 5:26 pm

If I may QUOTE Spogg .... love em. Thanks T.
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

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

Postby KG_is_back » Sun Apr 22, 2018 7:46 pm

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"
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

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

Postby tulamide » Sun Apr 22, 2018 10:19 pm

@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)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany


Return to User Examples

Who is online

Users browsing this forum: No registered users and 40 guests