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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:48:19+00:00 2026-06-06T14:48:19+00:00

There is each_pair method allows getting pairs of hash on every loop iteration: {

  • 0

There is each_pair method allows getting pairs of hash on every loop iteration:

{ 1 => "a", 2 => "b" }.each_pair do |key, value|
   # do something with #{key} and #{value}
end

How can index of current array element could be known on every loop iteration?

array.each do |element|
  # what is element index in array?
end

There is a boring solution using some incrementing iterator. But that iterator
should be assigned before the loop and should be manually incremented on every
iteration. It’s obviously boring.

It will be great if there is a some way to zip some array with 1.. array and
get array of tuples like ["b", "d", "e"] → [(1,"b"), (2,"d"), (3,"e")] and
than pattern matched each element of the pair in
| |` statement.

So, finally, what I am looking for is some function f, that:

f(["a"]) do |index, element|
  # index == 1, element == "a"
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-06-06T14:48:20+00:00Added an answer on June 6, 2026 at 2:48 pm

    You can loop over an array and get the current index by using Enumerable::each_with_index

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

Sidebar

Related Questions

There is so many option in each programming languages which can be mentioned in
For each of the modern browsers are there well known hooks I can tie
Is there a way to offset the 'each' iteration when using the Jade template
There's got to be something I'm missing. I've tried using $.ajax() and $.post() to
I'd like to create a list of method-object pairs. Each method is a function
Possible Duplicate: Object comparison in JavaScript Is there any method that takes in 2
Say if there is an array of 1000 hashes, with pairs like {:id =>
My structure: in each category there are texts. These texts are entries of its
I have the data structure For each item there is a record of it's
I have a div that contains a ul and in each li there is

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.