Variables Flashcards

1
Q

Variable Scope

A

Determines where in a program a variable is available for use. Is defined by where the variable is initialized or created. Is defined by a method definition or a block

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

Rule one of variable scope

A

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
3
Q

Variables

A

used to store information to be referenced and manipulated in a computer program

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

assignment operator

A

= (assigns value)

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

equality operator

A

== (checks if two things are equal)

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

block

A

piece of code that follows a method’s invocation, delimited by either curly braces {} or do/end

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

scope.rb

a = 5 # variable is initialized in the outer scope

3.times do |n| # method invocation with a block
a = 3 # is a accessible here, in an inner scope?
end

puts a

What is the value of a when it is printed to the screen?

A

The value of a is 3. This is because a is available to the inner scope created by 3.times do … end, which allowed the code to re-assign the value of a.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Convert the following code into a one liner:
# scope.rb

a = 5 # variable is initialized in the outer scope

3.times do |n| # method invocation with a block
a = 3
end

puts a

A

3.times { |n| a = 3 }

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

Constants

A
  • declared by capitalizing every letter in the variable’s name.
  • used for storing data that never needs to change
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Global Variables

A

declared by starting the variable name with the dollar sign ($).
-these variables are available throughout your entire app, overriding all scope boundaries.

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

Class variables

A
declared by starting the variable name with two @@ signs.
-accessible by instances of class as well as the class itself.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Instance variables

A
  • declared by starting the variable name with one @ sign.

- available throughout the current instance of the parent class.

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