Reputation: 21
I have a scenario
func1 do
x='abc'
func2 do
puts x
end
end
for this I get x = nil. Why is it so, and how to access external variable like x in inner block.
Upvotes: 1
Views: 403
Reputation: 83680
You will get "abc" only if func1 and func2 executes blocks you are passing to them (yielding or calling).
Check out an example
def func1
end
def func2
end
func1 do
x = "Hello World"
func2 do
puts x
end
end
#=> nil
def func3
yield
end
def func4
yield
end
func3 do
x = "Hello World"
func4 do
puts x
end
end
#=> Hello World
Upvotes: 1