Scope & Visibility

Scope gates: where local variables live and die

Ruby has a crisp rule most languages blur: class, module, and def each open a brand-new local scope β€” a scope gate. A local variable defined outside does not leak in, and one defined inside does not leak out. (Blocks are the deliberate exception β€” they close over their surroundings, which is why each can see outer variables. That's module 25.)

In an interview, say: "class, module, and def are scope gates β€” they start a fresh local scope. Blocks are not gates; they close over the enclosing scope. That distinction is why a constant defined at the top of a class is visible in its methods but a local variable isn't."