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 8977207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:19:10+00:00 2026-06-15T19:19:10+00:00

I am having difficulty understanding what makes the following behaviour possible (taken from the

  • 0

I am having difficulty understanding what makes the following behaviour possible (taken from the ruby pickaxe book):

def power_proc_generator
  value = 1
  lambda {value += value}
end

power_proc = power_proc_generator

3.times {puts power_proc.call} # => 2,4,8
3.times {puts power_proc_generator.call()} # => 2,2,2

I don’t see how the “power_proc” object allows the value to continue doubling as I would assume (wrongly it seems) that each call would reassign value to 1.

My question being why does “3.times {puts power_proc.call}” result “2,4,8” and not “2,2,2” ?

  • 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-06-15T19:19:11+00:00Added an answer on June 15, 2026 at 7:19 pm

    power_proc_generator returns a lambda which uses (and modifies) the value of a variable in the surrounding scope. This is known as a closure — the returned function “closes” over the value of the value variable. So each time you call the returned function, it multiplies value by two. The important part is that value stays around between calls to power_proc.call, so you’re modifying the existing variable.

    Also, to elaborate on the difference between printing power_proc_generator and power_proc.call — power_proc_generator returns a new function each time it’s called, which is why you never see value being increased. power_proc.call, on the other hand, continues calling the same function multiple times.

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

Sidebar

Related Questions

I am having difficulty understanding the part value : value == 0 ? How
I'm having a little difficulty understanding alias_method / alias_method_chain . I have the following
I'm having some difficulty in understanding the following WHERE clause in a T-SQL (SQL
This is from apple blocks docs and I am having difficulty understanding this please
I am having difficulty understanding why the following code is giving me the numbers
I am having difficulty understanding Sardinas- Patterson algorithm from the below slide: How do
I am using Python 2.6, and am having difficulty understanding why the following code
Possible Duplicate: argc and argv in main I'm having difficulty understanding the notation used
I'm a newbie to VB, and I am having difficulty understanding what the following
I am having difficulty understanding the proper syntax for the second default value in

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.