Welcome to day 4 of the 49 Days of Ruby! 🎉
In the past few days we have successfully installed Ruby on our local machine and have begun to experiment with IRB. Today, we are going to learn about the first important programming concept in our journey.
Today is dedicated to the variable.
What is a variable?
Wikipedia defines a variable as such:
In computer programming, a variable or scalar is a storage location (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.
Those are a lot of words. Let's break it down!
You know that you want to reference the text "Hello World!" in your program numerous times. You have a couple options. You could write out "Hello World!" each time, or you can save it somewhere and assign a reference point for it with a name you'll remember and just use that.
For our example of "Hello World!" that we wish to use multiple times in the same application, we could assign that text to a reference called greeting
and use greeting
instead.
How would that work?
Open up an instance of IRB, by running irb
from the command line.
In your IRB session write greeting = 'Hello World!'
and press enter.
Then on the next line write greeting
and press enter.
You should see something like this:
2.5.1 :001 > greeting = 'Hello World!'
=> "Hello World!"
2.5.1 :002 > greeting
=> "Hello World!"
On the first line we made the assignment to our new variable called greeting
. IRB prints out the assignment after we commit it. Then on the second line we return the variable's value by asking for it with executing greeting
. We see the "Hello World!" again.
You've created and used your first variable!, congrats!
Variables can hold all kinds of data. In this example we assigned a String
to us. We'll get to those data types in future days.
In the meantime, something else to be mindful of is when to use single quotes ('') or double quotes ("") when assigning a String
to your variable.
Is there a difference, you might ask? There is indeed a difference! Let's show the difference with an example:
2.5.1 :001 > greeting = 'Hello World!'
=> "Hello World!"
2.5.1 :002 > greeting
=> "Hello World!"
2.5.1 :003 > time_of_day = 'morning'
=> "morning"
2.5.1 :004 > "#{greeting}, a beautiful #{time_of_day} to you!"
=> "Hello World!, a beautiful morning to you!"
Lines 1 through 3 should look familiar to you by now. But, what is happening on line 4??
That is called interpolation. Let's look at Wikipedia again for a definition:
In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
In other words, string interpolation is the act of using variables inside a String
where the variable gets replaced by its value. This is exactly what is happening in line 4 of the example above.
On line 3 we create a new variable called time_of_day
and assign it the value of morning
. We then use that new variable and the already created greeting
variable to compose a new String
with their values.
How does this relate to when to use single-quotes vs. double-quotes? Well, in Ruby items put into single-quotes are never evaluated as potential variables, whereas with double-quotes they are. With single-quotes this is what would happen:
2.5.1 :005 > '#{greeting}, a beautiful #{time_of_day} to you!'
=> "\#{greeting}, a beautiful \#{time_of_day} to you!"
The variables greeting
and time_of_day
do not get replaced with their values. The special syntax of #{}
that we used to indicate that this item should be treated specially is ignored.
Now that you know about variables, we'll start covering all the sorts of things you can put into them tomorrow!
Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag #49daysofruby.
Top comments (0)