Skip to content

Instantly share code, notes, and snippets.

@mebezac
Created January 14, 2014 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mebezac/8424729 to your computer and use it in GitHub Desktop.
Save mebezac/8424729 to your computer and use it in GitHub Desktop.
Please help me understand variable scope in Ruby
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