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
[Ruby] Quote(') and double quote(") explained
Code: Select all
mystring = "Hello World"
mystring = 'Hello World'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 expectedCode: Select all
mystring = "Hello\nWorld" #will use seperate lines for Hello and World ( \n instructs a newline)Code: Select all
mystring = 'Hello\nWorld' #will just print Hello\nWorld, no new line is created.Code: Select all
mystring = "C:\music\rave\new\tulamide"
##results in:
C:musicave
ewulamideCode: Select all
mystring = 'C:\music\rave\new\tulamide'
##results in:
C:\music\rave\new\tulamideYou 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\tulamideRe: [Ruby] Quote(') and double quote(") explained
I do hope there'll be more to come.
Cheers
Spogg
Re: [Ruby] Quote(') and double quote(") explained
-
KG_is_back
- Posts: 1196
- Joined: Tue Oct 22, 2013 5:43 pm
- Location: Slovakia
Re: [Ruby] Quote(') and double quote(") explained
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"
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"
Re: [Ruby] Quote(') and double quote(") explained
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!