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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:04:36+00:00 2026-06-04T04:04:36+00:00

I understand everything that is happening in the following code except for the while

  • 0

I understand everything that is happening in the following code except for the while output section:

1 def doUntilFalse firstInput, someProc
2  input  = firstInput
3  output = firstInput
4  
5  while output
6    input  = output
7    output = someProc.call input
8  end
9  
10  input
11 end
12 
13 buildArrayOfSquares = Proc.new do |array|
14  lastNumber = array.last
15  if lastNumber <= 0
16    false
17  else
18    array.pop                         #  Take off the last number...
19    array.push lastNumber*lastNumber  #  ...and replace it with its square...
20    array.push lastNumber-1           #  ...followed by the next smaller number.
21  end
22 end
23
24 alwaysFalse = Proc.new do |justIgnoreMe|
25  false
26 end
27
28 puts doUntilFalse([5], buildArrayOfSquares).inspect

I understand while for the most part but for some reason I can’t see the forest through the trees in this code. Could someone explain what is happening in the while output part between lines 5 and 8. I have no doubt it is extremely simple but I’ve hit a wall with it. Any help much appreciated.

  • 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-04T04:04:38+00:00Added an answer on June 4, 2026 at 4:04 am

    output will become the return value of the procedure someProc, which is in turn passed as a parameter in line #28, as buildArrayOfSquares. This, in turn, will return false in a certain case; when this happens, the while loop will terminate.

    In detail, firstInput is [5], which becomes the first input. We call buildArrayOfSquares with [5]. Since 5 is not <= 0, we take 5 out, put in 25 and 4.

    Next iteration of while, output is [25, 4]. We continue. Back into buildArrayOfSquares. Take 4 off the end; push in 16, then 3. output is now [25, 16, 3].

    Next time, output is [25, 16, 9, 2]. Then [25, 16, 9, 4, 1]. Then [25, 16, 9, 4, 1, 0].

    And the next time, 0 <= 0, and buildArrayOfSquares returns false into output. The loop terminates. input is still [25, 16, 9, 4, 1, 0], which is what we presumably wanted.

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

Sidebar

Related Questions

Given the Ruby code line = first_name=mickey;last_name=mouse;country=usa record = Hash[*line.split(/=|;/)] I understand everything in
I have a very peculiar problem. I understand that while in the foreground the
I'm still new to programming/Java/Android so I'm trying to understand everything I do and
I've read the documentation , but I'm still not sure I understand everything. Especially
I read lot's of docs, tried everything and still can't understand why my template
I understand that Web Site Projects compile source on-the-fly, and Web Application Projects pre-compile
I understand that CoCreateInstance finds the COM server for the given class id, creates
I understand that in MVVM: the View knows about the ViewModel the ViewModel knows
I just wrote some code. It has my own custom made class. In that
Hi everyone I've got a huge issue that I don't quite understand why it's

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.