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