Scoping Flashcards
1
Q
If the local variable and the method are both in scope inside the block, which one
Ruby references first by default?
A
The local variable
e.g. name = "Lisa" def name "George" end
loop do
puts name
break
end
=> “Lisa”
2
Q
Can local variables that are initialized outside of a method be accessed from within the method definition?
A
No. They are not in the method’s scope.
In case of passing the variable as an argument to a method, they might point to the some object, but it would still be a different variable within the method.