Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Ruby Basics!
You have completed Ruby Basics!
Preview
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
What if we wanted to be able to
specify how long it should wait for?
0:00
puts and print both take text
that they want displayed.
0:02
sleep takes a number of seconds to
sleep for, but how are they doing that?
0:05
Before we can answer that we
need to explain variables.
0:10
Variables aren't a complex concept but
they're fundamental to programming.
0:13
You probably remember working with
variables in your Algebra class.
0:18
They were often denoted with with an X or
a Y and
0:21
were used to hold a variety
of possible values.
0:24
That is, variables are names for
values that can vary.
0:27
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