Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:16:59+00:00 2026-05-23T05:16:59+00:00

I am confused with the scope of a variable inside a block. This works:

  • 0

I am confused with the scope of a variable inside a block. This works:

f = 'new'
[1,2,3].each do |n| puts f * n end
#=> new newnew newnewnew

But this doesn’t:

[1,2,3].each do |n|
  a ||=[]
  a << n
end
a
#=>a does not exsit!

Why is this? And please put some resource on this topic for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:16:59+00:00Added an answer on May 23, 2026 at 5:16 am

    What’s confusing?

    In the first snippet f is created and then the each block is executed, which can see things outside itself (called enclosing scope). So it can see f.

    In the second snippet you create a inside the block, and so its scope is that block. Outside the block, a doesn’t exist.

    When you refer to a name (a, for instance) ruby will go from the current scope outwards, looking in all enclosing scopes for the name. If it finds it in one of the enclosing scopes, it uses the value that name is associated with. If not, it goes back to the most local scope and creates the name there. Subsequent name lookups will yield the value tied to that name.

    When a block ends, the names that were in that scope are lost (the values aren’t lost, just the names; values are lost when the garbage collector sees that no more names (or anything) refer to that value, and the gc collects the value to reuse its memory).


    If visualization is your thing, I find it helpful to think of scopes as a staircase, and at the beginning of a program, you are standing on the top step1. Every time a block is entered, you step down one step. You can see everything on the current step, and everything on steps above the one you’re on, but nothing on the steps below. When you refer to a variable name, you look around on the step you’re on to find it. When you see it, you use that value. If you don’t see it, you look to the next step above the one you’re on. If you see it, you use that value. You do this over and over till you’ve looked at the very top step but don’t see that name. If this happens, you create the name on the step you are standing on (and give it a value, if you’re looking it up for an assignment). The next time you look for that name, you’ll see it on the step you’re standing on, and use it there.

    When a block ends, you step up one stair step. Because you can’t see any names on the steps below, all the names on the step you were previously on are lost.

    If that helps you, think of it that way. If not, don’t.

    1 Actually you’re on the second step because you’re not in global scope, but to use names from global scope, you have to use a $ at the beginning of the name. So in the staircase example, if the name you are looking for has an $ at the beginning, you look directly at the top step. If not, you don’t look that far. However, this is kind of wrong, since all the stairs in the program would share the same top step, which is weird to think about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be obvious, but I'm getting a bit confused about PHP variable scope.
I'm very new to Perl and I'm confused with exactly how its variable scope
I thought I understood variable scope until I came across this bit of code:
I am confused with the variable scope in Javascript. I am trying to load
I'm new to JS and a bit confused on this subject, I hope you'll
Bit confused here, I have an on-demand instance but do I get charged even
Totally confused by this one... We have a WAMPServer installation set up, running a
I'm developing something for iOS with cocos2d. Now I have this CCLabelBMFont instance variable
I'm new to C++ and a bit confused regarding auto_ptr. I have a class
I'm a newbie in the Rails, but I can't figure this out. Rails maps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.