Variable scopes Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q
What is value of 'a' on the last line?
a = 7
def my_value(b)
  a = b
end
my_value(a + 5)
puts a
A

Answer:7

The result is 7. Once again, ‘a’ inside the my_value is not visible outside my_value, and ‘a’ at the top level is not visible inside my_value because methods are self-contained with respect to local variables.

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

What’s the value of ‘a’? WHY?
a = “Xyzzy”

def my_value(b)
b[2] = ‘-‘
end

my_value(a)
puts a

A

‘a’ = “Xy-zy”.
This is because string[] is a mutating method.

b[2] is referencing the ‘b’ in the argument and that is referencing the ‘a’ variable. Strings are mutable, numbers are not! Since we are actually modifying the string referenced by ‘b’, and ‘b’ references the same string as ‘a’, the result from printing a shows an update to the value of the string.

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

What is a and why?
a = “Xyzzy”

def my_value(b)
  b = 'yzzyX'
end

my_value(a)
puts a

A

‘a’ = “Xyzzy”
Assignment to a variable (an object) never mutates the object that is referenced. If you assign to b[2] like we did in the previous exercise, that’s a completely different operation; that actually mutates the content of the String referenced by b.

‘b’ is referencing a new object ‘yzzyX’, and is not related to ‘a’.

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

Are methods self-contained, or not?

e.g. Can methods access variables outside of it or not? How do you allow for such access?

A

Methods are self-contained.
Methods cannot access local variables outside of it, but it can if the local variable is passed into the method as an argument.

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

What’s the value of a? Why?

a = 7
array = [1, 2, 3]

array.each do |element|
a = element
end

puts a

A

a = 3

Methods are self-contained; Not so in blocks; blocks can use and modify local variables that are defined outside the block.

In this case, a starts out as 7, then we set a to 1, 2 and 3 in sequence. By the time we get to the puts, a has a value of 3.

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

What’s the value of a? Why?

a = 7
array = [1, 2, 3]

array.each do |element|
a = element
end

puts a

A

a = 3

Methods are self-contained; Not so in blocks; blocks can use and modify local variables that are defined outside the block.

In this case, a starts out as 7, then we set a to 1, 2 and 3 in sequence. By the time we get to the puts, a has a value of 3.

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

What’s the value of a?
array = [1, 2, 3]

array.each do |element|
a = element
end

puts a

A

An error occurs.
‘a’ was not defined outside of the block, so it is not available outside of the block.

If a was defined outside of the block first, it would be available inside of the block.

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

rand(100)

What is the min and max numbers?

A
min = 0
max = 99

rand returns a random number between 0 and the input value - 1

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

friends = [‘Sarah’, ‘John’, ‘Hannah’, ‘Dave’]

using the array above, use a ‘for’ loop to print out a greeting for each friend.

A

friends = [‘Sarah’, ‘John’, ‘Hannah’, ‘Dave’]

for name in friends
puts “Hello, #{name}”
end

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

What is ‘shadowing’, or ‘variable shadowing’ like in the following example?

x = 42
5.times{|x| puts x}

A

Shadowing is when you have two different local variables with the same name. It is said that the variable defined in the inner scope “shadows” the one in the outer scope (because the outer variable is now no longer accessible as long as the inner variable is in scope, even though it would otherwise be in scope).

So in this case, you can’t access the outer x variable in your block, because you have an inner variable with the same name.

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

What is the return results of each of the following?

1)
z = 42
5.times{|x| puts z}

2)
x = 42
5.times{|x| puts x}

A

1) 42, 42, 42, 42, 42
Because z is being ‘puts’ 5 times.

2) 0,1,2,3,4
Because x is being ‘puts’ 5 times(x = the number of the times counter)

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

What does this do?
loop do
number = rand(100)
puts number

if number.between?(0, 10)
break
end
end

A

‘number’ is assigned a random number between 0 & 99 and then printed to the console.

If the number is between 0 & 10, the loop is exited using ‘break’.

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