I’m one of those people who needs to see/understand a typical
implementation of something before I can grok how it works.
How are closures implemented? How does the closure know about its
environment? Since a block can use the various flavors of eval, it seems
to me that the closure needs access to everything.
I’m one of those people who needs to see/understand a typical
implementation of something before I can grok how it works.
How are closures implemented? How does the closure know about its
environment? Since a block can use the various flavors of eval, it
seems to me that the closure needs access to everything.
I would just say that for cases like this:
def a(x); eval("x"); end
Ruby keeps a list of local variables and its names in runtime and has
an internal method_missing dispatch logic which looks up undefined
methods
in the local variable table.
I wasn’t able to find the relevant source in reasonable time, but the
truth is out there. Someone more familiar with MRI sources than me
could
correct this.
How are closures implemented? How does the closure know about its
environment?
The closure is the environment.
Are you familiar with local variables allocated on a stack? Then instead
imagine stack frames allocated out of the heap, in a linked list.
Instead of being deallocated at the end of a function, they are
garbage-collected when there are no more references to them, just like
any other object.
z is a local variable in the scope which invokes define_method and
thus visible to the method body because you did not use def which
introduces a completely separate scope and not a nested scope as a
block does. From the scoping perspective your example is no different
than
z = 0
10.times.to_a.each do |y|
z += y
end
The only difference really is that by using define_method you retain another closure as method body from which the (additionally to
storing the lambdas in a variable after the method call). So you have
closure is the method body and it has access to z because that is
in the scope visible to the block passed to #define_method.
and 3. closure are the two lambdas returned which access the same z
also visible to the first closure.
Typically programming language implementations store local variables
on the stack so it does not make a difference whether we are talking
about a method argument or local variable - both would sit on the
stack. For the details you would have to look at the source.
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.