From: Peter Zotov Date: 2012-02-20T20:12:22+09:00 Subject: Re: How are closures implemented? Ralph Shnelvar писал 20.02.2012 12:37: > 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. > > Seems like magic to me. ... and it pretty much _is_ magic. > > I have looked at > http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby > but I am still not grokking it. > > Anyone have a link they can recommend on how closures are > implemented? > > "Any sufficiently advanced technology is indistinguishable from > magic." Arthur C. Clarke -- WBR, Peter Zotov.