- Read Tutorial
- Watch Guide Video
In this lesson, we are going to walk through how to use
Strings in a Ruby program.
string is a data type in Ruby and contains set of characters, typically normal English text (or whatever natural language you're building your program for), that you would write. A key point for the syntax of strings is that they have to be enclosed in single or double quotes if you want to use them in a program. The program will throw an error if they are not wrapped inside quotation marks.
Let's walk through three scenarios.
Missing Quotation Marks
In this code I tried to simply declare a string without wrapping it in quotation marks. As you can see this results in an error. This error is because Ruby thinks that the values are classes and methods.
In this code snippet we're printing out a string that we have properly wrapped in quotation marks. Please note that put single and double quotation marks work properly.
Storing Strings in Variables
Lastly in this code snippet we're storing a string inside of a variable and then printing the value out to the console.
We'll talk more about strings and string interpolation in subsequent lessons.