I am learning ruby at the moment. I am trying to understand the way closures work, and how they are different from functions. I am fully aware that closures should be implemented via proc or lambda.
I am trying to get an in depth understanding of ruby. As such I checking all kinds of unorthodox code. I am trying to understand why line 3 works while line 5 is an error.
x=123
def b(x)
p x
def a(u)
p x # why is this an error?!?!?
end
a 4
end
b 1
- If a can't access b's parameters, why doesn't it access the global x=123?
- Why does this work if I explicitly use change lines 1 & 5 to the global "$x"?
- Why does this work if I use a lambda explictly?
This is purely a learning exercise, I am doing this to understand what is going on beneath the hood.