Scoping Flashcards

You may prefer our related Brainscape-certified 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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

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