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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:50:35+00:00 2026-06-06T20:50:35+00:00

I have to search an item in an array and return the value of

  • 0

I have to search an item in an array and return the value of the next item. Example:

a = ['abc.df','-f','test.h']
i = a.find_index{|x| x=~/-f/}
puts a[i+1]

Is there any better way other than working with index?

  • 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-06T20:50:37+00:00Added an answer on June 6, 2026 at 8:50 pm

    A classical functional approach uses no indexes (xs.each_cons(2) -> pairwise combinations of xs):

    xs = ['abc.df', '-f', 'test.h']
    (xs.each_cons(2).detect { |x, y| x =~ /-f/ } || []).last
    #=> "test.h"
    

    Using Enumerable#map_detect simplifies it a litte bit more:

    xs.each_cons(2).map_detect { |x, y| y if x =~ /-f/ }
    #=> "test.h"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of two-item lists and need to search for things in
I have a binary search function I am passing a pointer character array, the
Ok, so I have this search box in which people typein a food item.
I often have the need to search a javascript array that contains objects. I
i have search at this websit : https://stackoverflow.com/search?page=2&tab=relevance&q=nsarraym%20objectatindex%20index%205%20beyond%20bounds%20for%20empty%20array%27 but the solve no suit me.
I have search screen which has 10 fields and involves around 5 to 6
I have search through the web to figure this out but no luck. I
I have search strings, similar to the one bellow: energy food olympics 2010 Terrorism
the div on website below that have search at the bottom and it always
I have an ASP.NET page that contains two div's. Both have search fields and

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.