If you're seeing this message, it means we're having trouble loading external resources on our website. Show
If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked. VariablesVariables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program. Assigning Value to VariablesNaming variables is known as one of the most difficult tasks in computer programming. When you are naming variables, think hard about the names. Try your best to make sure that the name you assign your variable is accurately descriptive and understandable to another reader. Sometimes that other reader is yourself when you revisit a program that you wrote months or even years earlier. When you assign a variable, you use the
Here we've assigned the value
As you can see, we've now stored the string Note:
Make sure you don't confuse the assignment operator ( Let's try a little something. Look at the following irb session.
What is the value of You'll notice that the value of Getting Data from a UserUp until now, you've only been able to assign data to variables from within the program. However, in the wild, you'll want other people to be able to interact with your programs in interesting ways. In order to do that, we have to allow the user to store information in variables as well. Then, we can decide what we'd like to do with that data. One way to get information from the user is to call the
After the code,
There we go! That's much prettier. Now we can use the
Variable ScopeA variable's scope determines where in a program a variable is available for use. A variable's scope is defined by where the variable is initialized or created. In Ruby, variable scope is defined by a method definition or by a block. They have different behaviors when it comes to variable scope. Variable Scope and Method DefinitionsWe'll learn about methods in the next chapter. For now, you can think of methods as pieces of reusable code that your program can execute many times during its run. Method definitions look like this:
Once we have the method defined, we can call it as many times as we need with different values for
In terms of variable scope, methods have self-contained scope. That means that only variables initialized within the method's body can be referenced or modified from within the method's body. Additionally, variables initialized inside a method's body aren't available
outside the method's body. It's a bit like an impenetrable bubble. Thus, in the above code, we can't use or change the Variable Scope and BlocksA block is a piece of code that follows a method's invocation, delimited by either curly braces
In the examples above, With blocks, the one rule that we want to emphasize is that: Inner scope can access variables initialized in an outer scope, but not vice versa. Looking at some code will make this clearer. Let's say we have a file called
What is the value of The value of Note that blocks can also be written with curly braces, though common practice is to only use them for one-liners: the method call and the block must fit on the same line. For instance, here's the
Curly brace blocks are identical to Let's try something else. We'll modify the original
What result did you get when running that program? You should have gotten an error to the tune of:
This is because the variable Be aware that not
all Put another way, the key distinguishing factor for deciding whether code delimited by
The answer is yes. The reason is because
the Types of VariablesBefore we move on, you should be aware that there are five types of variables. Constants, global variables, class variables, instance variables, and local variables. While you should not worry too much about these topics in depth yet, here is a brief description of each. Constants are declared by capitalizing every letter in the variable's name, per Ruby convention. They are used for storing data that never needs to change. While most programming languages do not allow you to change the value assigned to a constant, Ruby does. It will however throw a warning letting you know that there was a previous definition for that variable. Just because you can, doesn't mean you should change the value. In fact, you should not. Constants cannot be declared in method definitions, and are available throughout your application's scopes. Example of a constant declaration:
Global variables are declared by starting the variable name with the dollar sign ( Example of a global variable declaration:
Class variables are declared by starting the variable name with two Example of a class variable declaration:
Instance variables are declared by starting the variable name with one Example of an instance variable declaration:
Local variables are the most common variables you will come across and obey all scope boundaries. These variables are declared by starting the variable name with neither Example of a local variable declaration:
SummaryIn this chapter, we talked about how to use variables to store information for later use and how to get information from a user. We also showed that not all variables are created equal and that the scope in which a variable is defined changes its availability throughout the program. Now that you know the different types of variables and how to use them, let's put some of that knowledge into practice with some exercises. When you include a value in an assignment statement the value is called this?When an assignment statement includes a value, the value is called a literal because it literally is the value required by the assignment statement. A constant contains one permanent value throughout the execution of the program. It cannot be changed by any statement within the program.
What is a variable in programming?In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Typically, a program consists of instruction s that tell the computer what to do and data that the program uses when it is running.
What is the term given to the variable?A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same.
Is a name that references a value in the computer's memory?A variable is a piece of memory that has a name and a value.
|