How are Ruby Blocks implemented (in a compiler)
===

I want to implement a language construct similiar to Ruby´s blocks. So

    main()
    {
        i = 123
        array.function(|x| { puts x + i; }) //the block needs access to the local variable i
    }

should compile fine. I looked at the source code for 1 hour but since I´m not that good at C I still have no idea how they implement it. My first thought was it is a nested function which address gets passed as a parameter, but I´m not sure how the nested function has access to the local variable because it seems in most languages this isnt possible. If anyone can give me an idea of how to implement this I would be very glad