Scopes
After learning how to write methods, we need to have a look at a concept called scopes.
In the following interactive exercise, we'll learn what this means.
What's the output of following script:
greeting = "Hello!"
puts greeting
Hello!
def greet_in_french
greeting = "Bonjour!"
puts greeting
end
greet_in_french
Bonjour!
greeting = "Hello!"
def greet_in_czech
greeting = "Ahoj!"
puts greeting
end
greet_in_czech
puts greeting
Ahoj!
Hello!
greeting.
greeting = "Hello!"
def greet_in_japanese
greeting = "ใใใซใกใฏ" # Kon'nichiwa
puts greeting
end
greeting = "Good day!"
greet_in_japanese
puts greeting
ใใใซใกใฏ
Good day!
greeting = "Hei der!"
def greet_in_norwegian
puts greeting
end
greet_in_norwegian
There is a way how we can read variables from a global scope.
This is covered in a different lesson. ๐ทโโ๏ธ
What questions do you have?