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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:13:11+00:00 2026-05-13T20:13:11+00:00

In this code, I create an array of strings 1 to 10000: array_of_strings =

  • 0

In this code, I create an array of strings “1” to “10000”:

array_of_strings = (1..10000).collect {|i| String(i)}

Does the Ruby Core API provide a way to get an enumerable object that lets me enumerate over the same list, generating the string values on demand, rather than generating an array of the strings?

Here’s a further example which hopefully clarifies what I am trying to do:

def find_me_an_awesome_username
  awesome_names = (1..1000000).xform {|i| "hacker_" + String(i) }
  awesome_names.find {|n| not stackoverflow.userexists(n) }
end

Where xform is the method I am looking for.
awesome_names is an Enumerable, so xform isn’t creating a 1 million element array of strings, but just generating and returning strings of the form “hacker_[N]” on demand.

By the way, here’s what it might look like in C#:

var awesomeNames = from i in Range(1, 1000000) select "hacker_" + i;
var name = awesomeNames.First((n) => !stackoverflow.UserExists(n));

(One Solution)

Here is an extension to Enumerator that adds an xform method. It returns another enumerator which iterates over the values of the original enumerator, with a transform applied to it.

class Enumerator
  def xform(&block)
    Enumerator.new do |yielder|
      self.each do |val|
        yielder.yield block.call(val)
      end
    end
  end
end

# this prints out even numbers from 2 to 10:
(1..10).each.xform {|i| i*2}.each {|i| puts i}
  • 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-13T20:13:11+00:00Added an answer on May 13, 2026 at 8:13 pm

    Ruby 2.0 introduced Enumerable#lazy which allows one to chain map, select, etc…, and only generate the final results at the end with to_a, first, etc… You can use it in any Ruby version with require 'backports/2.0.0/enumerable/lazy'.

    require 'backports/2.0.0/enumerable/lazy'
    names = (1..Float::INFINITY).lazy.map{|i| "hacker_" + String(i) }
    names.first # => 'hacker_1'
    

    Otherwise, you can use Enumerator.new { with_a_block }. It’s new in Ruby 1.9, so require 'backports/1.9.1/enumerator/new' if you need it in Ruby 1.8.x.

    As per your example, the following will not create an intermediate array and will only construct the needed strings:

    require 'backports/1.9.1/enumerator/new'
    
    def find_me_an_awesome_username
      awesome_names = Enumerator.new do |y|
        (1..1000000).each {|i| y.yield "hacker_" + String(i) }
      end
      awesome_names.find {|n| not stackoverflow.userexists(n) }
    end
    

    You can even replace the 100000 by 1.0/0 (i.e. Infinity), if you want.

    To answer your comment, if you are always mapping your values one to one, you could have something like:

    module Enumerable
      def lazy_each
        Enumerator.new do |yielder|
          each do |value|
            yielder.yield(yield value)
          end
        end
      end
    end
    
    awesome_names = (1..100000).lazy_each{|i| "hacker_#{i}"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code to create a file. public void writeToFile(byte[] array) { try
This is the code in my view: echo $this->Form->create('Chart'); echo $this->Form->input('username', array('label'=>('Usernames List'), 'empty'=>('Select
I am having a problem when I create an array of strings, this only
I want to create an array of strings. Here is the code: #include <stdio.h>
I have an array created with this code: var widthRange = new Array(); widthRange[46]
Why, this code create 2 the same columns in grid (Color and Color). How
I'm using this code to create multiple functions wrappers using variadic templates: // Compile
I've written this code to create a list of email addresses from branch numbers.
I wrote this code to create a custom annotation image - (MKAnnotationView *)mapView:(MKMapView *)mapView
I have this code to create an ATOM feed Dim xmlResult As New StringBuilder

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.