First, I will explain the difference between character strings and numbers.
Hello Hello! Today he calls non-numeric numbers such as Konnichiha strings. When typing '(Single quote) Or "(Double quotation) Enclose in.
For example, "Hello" 'Hello'
Numbers such as 22, 777, and 8. You don't have to enclose it in single or double quotes.
In the case of the string by connecting the Hello door Taro, if you want to hello Taro is And the "Hello" + "Taro"
In the case of numerical values, when you want to add 5 and 9 5 + 9
So what happens if you add a string and a number?
Hello, Today is 24 days. When making the sentence by addition
"Hello, it is today" "The Day." + 24 +
Then I get an error. This is because numbers and strings cannot be calculated directly.
Then, what to do is to convert the numerical value to a character string using the to_s method.
"Hello, today is" + 24.to_s + "is the date." To clean up can be calculated.
On the contrary, if you want to convert the character string to a numerical value, use the to_i method.
"500" If it is surrounded by single quotation marks or double quotation marks, it will be treated as a character string. If you want to convert it to a numerical value, use "500" .to_i.
That's all for today, and next time I'll write about methods.
Recommended Posts