---
title: "How Ruby Implements Closures: What Really Happens When You Call lambda"
url: https://daily.dev/posts/how-ruby-implements-closures-what-really-happens-when-you-call-lambda-iuict0hih
source_url: https://rubystacknews.com/2026/06/24/how-ruby-implements-closures-what-really-happens-when-you-call-lambda
type: article
source: "Ruby Flow"
published: 2026-06-25T08:12:08.390Z
updated: 2026-06-25T08:12:29.195Z
tags: ["ruby"]
reading_time: 4
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How Ruby Implements Closures: What Really Happens When You Call lambda

**[Ruby Flow](https://daily.dev/sources/rubyflow)** · 4 min read · 0 upvotes · 0 comments

## Summary

A deep dive into how Ruby's YARV virtual machine implements closures under the hood. When a lambda or proc captures a local variable, Ruby copies the current stack frame environment to the heap, creating an rb_env_t object. Crucially, Ruby then redirects the Environment Pointer (EP) to the heap copy, so all subsequent variable accesses — including mutations — go through the heap. This explains why multiple lambdas in the same scope share the same captured variables (they reuse the same rb_env_t), why variable modifications after lambda creation are visible inside the closure, and why local variables survive after their enclosing method returns. Internally, both lambda and Proc.new produce an rb_proc_t object distinguished only by an is_lambda boolean flag.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://rubystacknews.com/2026/06/24/how-ruby-implements-closures-what-really-happens-when-you-call-lambda>

## Similar posts on daily.dev

- [Understanding Ruby Proc Internals Through proc.c](https://daily.dev/posts/understanding-ruby-proc-internals-through-proc-c-qqkenslqb) · RUBYLAND · 0 upvotes · 0 comments
- [YARV’s Internal Stack and Your Ruby Stack](https://daily.dev/posts/yarv-s-internal-stack-and-your-ruby-stack-gxehfgp5e) · RUBYLAND · 0 upvotes · 0 comments
- [Compiling a Call to a Block](https://daily.dev/posts/compiling-a-call-to-a-block-eerqkfziy) · RUBYLAND · 0 upvotes · 0 comments

---

Tags: [#ruby](https://daily.dev/tags/ruby)

[View this post on daily.dev](https://daily.dev/posts/how-ruby-implements-closures-what-really-happens-when-you-call-lambda-iuict0hih)
