Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Variables can hold numbers, text, a calendar date, or any other piece of data that can be stored in your computer's memory.
- Variables in a programming language are like variables in algrebra, but they can hold other things besides numbers. They can hold text, or a calendar date, or any other piece of data that can be stored in your computer's memory.
- When you assign a value to a variable, you're giving that value a name that you can refer to it by.
- You assign a value to a variable with a single equals sign:
=
number = 4
greeting = "hello"
- We can then use that variable anywhere we might use the original piece of data.
puts number # 4
puts greeting # "hello"
puts number + 2 # 6
puts 12 - number # 8
- If we change the value the variable holds, the remainder of the program will use that new value instead of the old one.
number = 6
greeting = "hi"
puts number # 6
puts greeting # "hi"
puts number + 2 # 8
puts 12 - number # 6
- We can even replace the value a variable holds in the middle of a program.
- Valid variable names - same as method names.
- All lower case
- Numbers are legal but rarely used
- Snake case - separate words with underscores.
word = "hi"
multiple_words = "hi there"
-
0:00
Back in our temp.rb file, our wait method always waits three seconds.
-
0:05
What if we wanted to be able to specify how long it should wait for?
-
0:11
Puts and print both take text that they want displayed.
-
0:14
Sleep takes a number of seconds to sleep for, but how are they doing that?
-
0:19
Before we can answer that we need to explain variables.
-
0:22
Variables aren't a complex concept but they're fundamental to programming.
-
0:27
You probably remember working with variables in your Algebra class.
-
0:30
They were often denoted with with an X or a Y and
-
0:33
were used to hold a variety of possible values.
-
0:36
That is, variables are names for values that can vary.
-
0:40
In this equation Y = X + 5, if we set X equal to 1, then Y would equal 6.
-
0:47
If we set X equal to 10, then Y would equal 15, and so on.
-
0:52
Variables in a programming language are like that, but
-
0:55
they can hold other things besides numbers.
-
0:57
They can hold texts or a calendar date, or
-
0:59
any other piece of data that can be stored in your computer's memory.
-
1:03
Let's create a new Ruby source file so
-
1:05
we can play around with variables a little bit.
-
1:07
I'll click on the file menu, choose New File, and
-
1:10
I'll type the name of variables.rb.
-
1:14
When you assign a value to a variable,
-
1:16
you're giving that value a name that you can refer to it by.
-
1:20
You assign a value to a variable with single equal sign.
-
1:23
So, let's create a new variable named number and
-
1:27
assign a value to it with =, and we'll give it a value of 4.
-
1:31
Then we'll create another variable named greeting, and
-
1:34
we'll assign a value to that with equals.
-
1:36
And this one, we're going to assign a string value to.
-
1:39
So we open our string with a quotation mark.
-
1:42
We'll make the string say hello.
-
1:44
And we'll end the string with another quotation mark.
-
1:47
We can then use that variable anywhere we might use the original piece of data.
-
1:51
So let's try making a call to puts to print a value out.
-
1:55
And instead of passing puts the number 4, I'm going to pass it the variable number.
-
2:03
Let's try saving that and running it.
-
2:05
Down here in the console I will type ruby,
-
2:11
space, and then our new source code file name, variables.rb.
-
2:16
And you can see that it prints out the number 4.
-
2:18
It prints out the value that the variable number holds.
-
2:23
Let's try doing the same with the greeting variable.
-
2:25
We'll call it the puts method, and
-
2:27
as an argument we'll pass it the variable greeting.
-
2:30
Save that and try running it again.
-
2:33
And first it will print the value of number followed by the value of greeting.
-
2:36
We can even use variables within other expressions.
-
2:40
Let's make another call to puts, and
-
2:43
this time we'll pass it the result of adding 2 to the value in number.
-
2:47
Save that, try running it.
-
2:53
And you see that first it prints the original value of number four,
-
2:56
then the value of greeting, hello, and
-
2:58
then it prints the value resulting from adding 2 to the value in number.
-
3:04
You get 6 as a result.
-
3:06
It's worth noting that math operations like this don't affect the value
-
3:10
that's in the variable, unless you assign that new value back to the variable.
-
3:15
So if we were to call puts again here and
-
3:18
pass number to it and try running that again.
-
3:22
You'll see that the original value held in number is 4,
-
3:26
we add 2 to it and print that out here and get the result 6.
-
3:30
But you can see that the value contained within the number variable hasn't changed,
-
3:34
it's still 4 and that's what gets printed last.
-
3:39
Let's do one more operation with the value and
-
3:43
number we'll subtract, 12 minus number.
-
3:46
Try saving that.
-
3:47
Run it.
-
3:50
And you can see that it finally prints the value 8 because number still holds 4,
-
3:56
and 12 minus 4 is 8.
-
3:59
If we change the value that a variable holds,
-
4:02
the remainder of the program will use that new value instead of the old one.
-
4:06
So let's try changing the value and number to 6 here at the top of the program, and
-
4:10
we'll try changing the string in the greeting variable from hello to hi.
-
4:14
Try running that.
-
4:17
And you can see the results reflected in the output here,
-
4:20
it prints out the value in number which is now 6 instead of four.
-
4:24
It prints out the value of your greeting which is now hi, instead of hello.
-
4:28
Number plus 2 is now 8 because 6 plus 2 is 8.
-
4:32
And 12 minus number is 6 because number still holds 6.
-
4:38
We can even replace the value a variable holds in the middle of a program.
-
4:42
So here after number plus 2, let's try assigning a new value to number.
-
4:46
We'll say number equals 10.
-
4:48
Try running that.
-
4:50
And you'll see that the program starts
-
4:55
printing 6, because that's the value that number holds at first.
-
5:00
And here it prints 8, because 6 plus 2 is 8.
-
5:03
But here we changed the value to 10.
-
5:06
And so, 12 minus 10 gives us a result of 2.
-
5:09
We were able to change the value that our number variable held
-
5:13
in the middle of our program.
You need to sign up for Treehouse in order to download course files.
Sign up