Tsallaka zuwa abun ciki
Justice Tech School

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 $count

Result

13
no x
1

Exercise

Global prefix.

count = 0

Quiz Test

Can a method see outer locals?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.