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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:52:13+00:00 2026-05-24T04:52:13+00:00

I stole my title from this post: Executes a function until it returns a

  • 0

I stole my title from this post: Executes a function until it returns a nil, collecting its values into a list

That question refers to Lisp and is, frankly, over my head. However, I think that his question–translated into Ruby–is exactly my own:

What’s the best way to create a conditional loop in [Ruby] that executes a function until it returns NIL at which time it collects the returned values into a list?

My current, clunky approach is this:

def foo
   ret = Array.new
   x = func() # parenthesis for clarity (I'm not a native Ruby coder...)
   until x.nil?
     ret << x
     x = func() 
   end
   ret
end

This code snippet will do what I want…but I know there is a cleaner, more idiomatically Ruby approach…right?

  • 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-24T04:52:14+00:00Added an answer on May 24, 2026 at 4:52 am

    Funny how nobody suggested Enumerator and its take_while method, to me it seems just fit:

    # example function that sometimes returns nil
    def func
      r = rand(5)
      r == 0 ? nil : r
    end
    
    # wrap function call into lazy enumerator
    enum = Enumerator.new{|y|
      loop {
        y << func()
      }
    }
    
    # take from it until we bump into a nil
    arr = enum.take_while{|elem|
      !elem.nil?
    }
    
    p arr
    #=>[3, 3, 2, 4, 1, 1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to use an List to store the title, path,... from Documents. I
I am trying to store HTML posted from a textarea into a database. I
As the title explains, I want to maintain an information across requests from multiple
I'm developing a store which gets its product info from lots of xml feed,
I'm looking for help identifying this design pattern and learning the typical vocabulary it
I don't know if this is to do with Raphael, ColorBox or jQuery. Here's
Integrating SimpleModal with ASP.NET and MasterPages This is a continuation of a previous thread
I am reading a tutorial on how to insert and update data into a
I have a text box in a page. Whose visibility is set false from
I have a Javascript object that (very simplified) looks like this if('undefined' !== typeof(listCtrls)){

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.