|
Every time you want to assign a string to a variable, remember to use the quotes.
Let's try imagining we're trying to write a very similar application to keep tracking of books you borrow from your friends and need to
give back.
So let's assume Bob gave you his copy of "Fight Club" and told you that you'd have to give it back in 2 weeks and Mark gave you his C++ programming for beginners textbook for 3 week.
You'd want to check the application from time to time to see the day you got each book and how much time is left till you need to give them back.We will assume both books have been borrowed during the same month and will also be returned the same month for the simplicity of the code.
Most of the code would be this:
firstname="Bob"
secondname="Mark"
bob_start=10
mark_start=5
bob_return=24
mark_return=26
puts "#{firstname} gave me the book on #{bob_start} this month.I have #{bob_return-bob_start} days left"
This could've also been written like this:
puts "#{firstname} gave me the book on #{bob_start} this month"+" "+"I have #{bob_return-bob_start} days left"
We use the "+" operator to concatenate the 2 strings, with one space between them.
You also now learned how to display values of variables inside printed screen messages by using the # sign before a variable enclosed in { }.
The difference between single-quoted enclosed strings and double quoted enclosed strings
Enclosing a string in single quotes means that only 2 escape sequences will be recognized if included inside the string: the escaped backslash (\\) and the escaped (\') single quote.
If you don't know what escape sequences are used for: if you want a certain character besides a letter or number to correctly display inside a quoted string, you must "escape" it with a backslash first, otherwise it mightbe interpreted as something else that what you intended.
Since we are talking about single quoted strings now, think of this:
x='this is a single quoted string'
y='this is John's single quoted string'
z='you can find the ebook in d:\\ebooks'
|