Created
January 14, 2014 20:06
-
-
Save mebezac/8424729 to your computer and use it in GitHub Desktop.
Please help me understand variable scope in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Please enter num1" | |
num1 = 13 | |
puts "At the beginning I am: #{num1}" #num1 = 13 | |
def change(num) | |
num1 = num | |
puts "Inside of the change method I am: #{num1} " #num1 = 47 because num1 is local inside change | |
end | |
change 47 | |
puts "But outside of the change method I am still: #{num1}" #num1 = 13 because change did nothing to the original num1 | |
array = ["crazy example"] | |
array.each do |e| | |
num1 = e | |
end | |
puts "But look now, I've completely changed num1 to: #{num1}" #num1 = crazy example because it was changed in array.each |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment