Ruby Variables Flashcards

1
Q

Variables

A

Variables 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Camel_Case

A

used for assigning variables

first_name = “Jameson”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

gets

A

gets stands for “get string”

When you use it, the program waits for the user to 1) type in information and 2) press the enter key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

gets.chomp

A

you can put .chomp after any string to remove the carriage return characters at the end.

irb :001 > name = gets
Bob
=> “Bob\n”

irb :001 > name = gets.chomp
Bob
=> “Bob”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

variable scope

A

A 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 block. A block is a piece of code following a method invocation, usually delimited by either curly braces {} or do/end. Be aware that not all do/end pairs imply a block*.

Inner scope can access variables initialized in an outer scope, but not vice versa.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

five types of variables

A

Constants, global variables, class variables, instance variables, and local variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Constants

A

Constants are declared by capitalizing every letter in the variable’s name. 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 methods, and are available throughout your application’s scopes.

MY_CONSTANT = ‘I am available throughout your app.’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Global variables

A

Global variables are declared by starting the variable name with the dollar sign ($). These variables are available throughout your entire app, overriding all scope boundaries. Rubyists tend to stay away from global variables as there can be unexpected complications when using them.

$var = ‘I am also available throughout your app.’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Class variable

A

Class variables are declared by starting the variable name with two @ signs. These variables are accessible by instances of your class, as well as the class itself. When you need to declare a variable that is related to a class, but each instance of that class does not need its own value for this variable, you use a class variable. Class variables must be initialized at the class level, outside of any methods. They can then be altered using class or instance methods.

@@instances = 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Instance variable

A

Instance variables are declared by starting the variable name with one @ sign. These variables are available throughout the current instance of the parent class. Instance variables can cross some scope boundaries, but not all of them. You will learn more about this when you get to OOP topics, and should not use instance variables until you know more about them.

@var = ‘I am available throughout the current instance of this class.’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Local variable

A

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 $ nor @, as well as not capitalizing the entire variable name.

var = ‘I must be passed around to cross scope boundaries.’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly