The range in which a variable can be used. If you try to use a variable that is out of scope, you will get an error.
Let's see an example where Ruby and Python actually have different scopes.
name = 'Mike'
def greeting()
puts ('Hello ' + name)
end
greeting() #=> error (undefined local variable)
In Ruby, only the variables defined in the method can be used in the method. So this time, the greeting method is trying to use the out-of-scope variable name, which causes an error.
name = 'Mike'
def greeting():
print('Hello ' + name)
x = 2
greeting() #=> Hello Mike
print(x) #=> error
In Python, the variable name affects the greeting method, so it can be executed without error.
However, the variable x defined inside the method cannot be used outside the method, so an error occurs.
Recommended Posts