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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:04:32+00:00 2026-06-05T15:04:32+00:00

If I want a part of an array I can use [] or split

  • 0

If I want a part of an array I can use [] or split:

arr = [1,2,3,4,5]
arr[1..3]
=> [2, 3, 4]

But is there a ‘general’ version of []? Can I apply it to any Enumerator?

enum = arr.each
enum.xxx_method(1..3) # is equal to arr[1..3].each

Of course you can use arr[1..3] directly. But I’m seeking a general way to handle any enumerator.

  • 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-05T15:04:33+00:00Added an answer on June 5, 2026 at 3:04 pm

    If you have an enumerator, you can count on Enumerable methods drop and take:

    # abstract if necessary as enum_slice(range)
    enumerator.drop(2).take(3)
    

    If that enumerator is an array you don’t need to traverse it, check the method Array#lazy_slice that I asked to be added to enumerable_lazy in relation with your previous question:

    require 'enumerable/lazy'
    
    class Array
      def lazy_slice(range)
        Enumerator.new do |yielder|
          range.each do |index|
            yielder << self[index]
          end
        end.lazy
      end
    end
    
    some_big_array = (0..10000).to_a # fake array, it won't generally be a range
    p some_big_array.lazy_slice(9995..10000).map { |x| 2*x }.to_a
    #=> [19990, 19992, 19994, 19996, 19998, 20000]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to put an image on a button, but I want part of
I have a multi-part array that I want to grab a piece of and
I want to create in C++ an array which can hold objects of diffrent
Let's say I want to move a part of an array right by 1.
I want to use a URL like this: site.com/car-list/param1/value1/param2/value2 car is variable and can
I want to create a 2D array which can be used for two different
I'm looking to create an associative array in JavaScript, but use constants defined as
I want date part from an instance of ActiveSupport::TimeWithZone . I used to_date function
I want that part 1 onclick div style changes and part 2 again on
I want to delete part of a record. For example, a column called message

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.