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

  • Home
  • SEARCH
  • 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 1114815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:03:51+00:00 2026-05-17T03:03:51+00:00

How may i fetch next and before current element from array while iterating array

  • 0

How may i fetch next and before current element from array while iterating array with each.

array.each do |a|
   # I want to fetch next and before current element.
end
  • 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-17T03:03:51+00:00Added an answer on May 17, 2026 at 3:03 am

    Take look at Enumerable#each_cons method:

    [nil, *array, nil].each_cons(3){|prev, curr, nxt|
      puts "prev: #{prev} curr: #{curr} next: #{nxt}"
    }
    
    prev:  curr: a next: b
    prev: a curr: b next: c
    prev: b curr: c next: 
    

    If you like, you can wrap it in new Array method:

    class Array
      def each_with_prev_next &block
        [nil, *self, nil].each_cons(3, &block)
      end
    end
    #=> nil
    
    array.each_with_prev_next do |prev, curr, nxt|
      puts "prev: #{prev} curr: #{curr} next: #{nxt}"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fetch data from three tables that are Products , Attributes and
I want to fetch all the strings between _( ) from my file. How
May I install a search provider/accelerator remotely in Internet Explorer 8? I dont want
May I know how could I pass the value of driver from code behind
May I know if there is any way to remove non-alphabetical symbols from a
How to get the next row from the Database table? Such that the row
I want fetch the parameters of my hard disk. Using the technique described here
I'm afraid this may be a very stupid question. I want to refer people
Using xpath how do i fetch 10 nodes(actually it may be n nodes where
I want to fetch last 4 weeks results including this week. so let say

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.