Variable scopes Flashcards
What is value of 'a' on the last line? a = 7 def my_value(b) a = b end my_value(a + 5) puts 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.
What’s the value of ‘a’? WHY?
a = “Xyzzy”
def my_value(b)
b[2] = ‘-‘
end
my_value(a)
puts 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.
What is a and why?
a = “Xyzzy”
def my_value(b) b = 'yzzyX' end
my_value(a)
puts 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’.
Are methods self-contained, or not?
e.g. Can methods access variables outside of it or not? How do you allow for such access?
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.
What’s the value of a? Why?
a = 7 array = [1, 2, 3]
array.each do |element|
a = element
end
puts 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.
What’s the value of a? Why?
a = 7 array = [1, 2, 3]
array.each do |element|
a = element
end
puts 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.
What’s the value of a?
array = [1, 2, 3]
array.each do |element|
a = element
end
puts 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.
rand(100)
What is the min and max numbers?
min = 0 max = 99
rand returns a random number between 0 and the input value - 1
friends = [‘Sarah’, ‘John’, ‘Hannah’, ‘Dave’]
using the array above, use a ‘for’ loop to print out a greeting for each friend.
friends = [‘Sarah’, ‘John’, ‘Hannah’, ‘Dave’]
for name in friends
puts “Hello, #{name}”
end
What is ‘shadowing’, or ‘variable shadowing’ like in the following example?
x = 42
5.times{|x| puts x}
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.
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}
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)
What does this do?
loop do
number = rand(100)
puts number
if number.between?(0, 10)
break
end
end
‘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’.