Locked lesson.
About this lesson
Storing information in variables is easy!
Exercise files
Download this lesson’s related exercise files.
Variables.docx58.8 KB Variables - Solution.docx
58.8 KB
Quick reference
Variables
Variables are containers that hold things.
When to use
Use variables all the time to store things.
Instructions
Variable names should be lower case, and multiple words should be separated by underscores. Don't use uppercase letters in variable names.
Variable names should be descriptive.
To create a variable:
my_name = "John Elder"
my_number = 41
Hints & tips
- Variables let us store things
- They're like containers that we can put things in
- 00:04 In this video I want to talk about variables.
- 00:06 And variables are just probably the most important thing in
- 00:10 any programming language.
- 00:11 And you're going to use these forever always every day.
- 00:15 So a variable is just, think of it like a bucket, and
- 00:18 you can put things into it and then take them out later on.
- 00:21 It's a box that you can contain things in.
- 00:23 It's a box you can put things in.
- 00:25 So to create a variable, you just name it, and
- 00:28 let's call this name not very good name, but that's our variable.
- 00:31 And to put something in our variable, we just do = and
- 00:34 I'm just going to type John Elder.
- 00:37 If we save this, now we have our variable, we've created it.
- 00:40 So if we save it and run this program now, nothing happens because we haven't
- 00:45 told our program to do anything with that variable.
- 00:47 We've just created it.
- 00:48 So whenever you create a name for a variable,
- 00:51 it's a good idea to use a description.
- 00:54 So name is not a very good one.
- 00:56 Let's say we were building a program for a shopping cart.
- 00:59 This might be customer_name, that's a better variable name.
- 01:04 So you want to be descriptive,
- 01:05 as descriptive as possible with your variable names.
- 01:07 Another thing is they have to be lowercase.
- 01:10 They have to start with lowercase.
- 01:12 You cannot start this with an uppercase letter.
- 01:15 You see it changes and
- 01:16 the reason why is because Ruby uses uppercase variables to name classes.
- 01:21 And we're going to talk about classes much later on in the course.
- 01:24 But for now just understand you can't have an uppercase variable name.
- 01:27 Also when you have two words customer and
- 01:30 name, the convention in Ruby is to use this underscore.
- 01:33 A lot of programming languages use something called camel case,
- 01:36 which is this.
- 01:38 It's like a hump, like a camel has a hump.
- 01:40 So underscore customer, upperscore name.
- 01:42 Now, you can do this, you're not going to get an error or anything.
- 01:45 But the convention in Ruby is to use underscores, everybody does it so
- 01:49 you should too, so here we go.
- 01:51 Now this variable has been created.
- 01:53 We didn't have to initialize it.
- 01:54 Now in a lot of programming language you have to
- 01:59 initialize customer_name before you can use it.
- 02:04 Not so with Ruby, all you have to do is name it, slap something in and
- 02:08 it's ready to go, which is a great, great thing about Ruby.
- 02:11 So to use this, we can just we can do whatever we want.
- 02:14 And as you see, as I start to type it, our program already knows it exists.
- 02:18 So I can hit tab and it'll just pop it right in.
- 02:21 So if we save this, come here and run it, we see John Elder so very, very cool.
- 02:26 You can see here I've put a string in customer_name John Elder,
- 02:31 you could just as easily create something called price = 1999.
- 02:37 Now if we puts price, we get 1999.
- 02:42 So notice this is a number, this is a float, we put a float in a variable.
- 02:46 We didn't have to tell our program, hey,
- 02:48 we're going to use numbers in this variables.
- 02:50 A lot of programming languages you have to declare your variables to be number
- 02:55 associated, not so with Ruby.
- 02:56 You just slap your thing in there and go.
- 02:58 Likewise we can change things, we can go customer_name
- 03:04 = Bill puts customer_name, run it.
- 03:11 The first time we output it, it was John Elder.
- 03:14 We changed it down further in the program output it again and it changed to Bill.
- 03:19 So these are fluid, you can put things in, you can take them out,
- 03:23 you can replace them, we could of changed this to 21, save it, run it again.
- 03:29 The first time it's John Elder, the second time it's 21.
- 03:32 So you can change the data type of a variable.
- 03:35 Up here the data type was string because we put a string in there.
- 03:39 Down here the data type was integer because we put a number in there,
- 03:42 so very flexible with what you can do with variables with Ruby, which is very cool.
- 03:47 And like I said, you're just going to use these forever,
- 03:49 variables are just a cornerstone of all programming.
- 03:51 They're going to be in every program you ever write, yeah.
- 03:54 In the next video, we're going to talk about assignment operators.
Lesson notes are only available for subscribers.