Variable Scope
Methods have their own scope. Blocks see outer locals. $global and @instance variables have wider reach.
Example
x = 10
[1, 2].each { |i| x += i }
puts x
def show
defined?(x) ? "sees x" : "no x"
end
puts show
$count = 0
$count += 1
puts $countResult
13 no x 1
Exercise
Global prefix.
count = 0Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.