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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:10:06+00:00 2026-06-09T14:10:06+00:00

Edit: All fixed now. Beginners mistake but I thought I had troubleshot well enough

  • 0

Edit: All fixed now. Beginners mistake but I thought I had troubleshot well enough for my beginners level. However I failed to remember the most basic thing to check.

I’m trying to find the location of X within an array. According to a website it should work just like this:

a = [ "a", "b", "c" , "d"]

a.index("d")

However this does not return anything on its own. However I’ve added an if statement to it:

a = [ "a", "b", "c" , "d"]

if a.index("d") == 3
  puts "ok"

else 

puts "error"

end

And this works. However obviously this isn’t optimal since I won’t be guessing between just 4 array elements but many thousands. Is the first code supposed to work? And if not how do I get the array number?

Secondary question: After searching for this value “d” (above code) and getting its position. How do I take the position information, put it into an integer so I can apply math to it. And then fetch the new array?

Additionally, it would also be best if the value being searched for can be controlled outside of this index. How do I make the index point to a string instead that contains what to search for?

Thanks and sorry I am completely new to programming. I am doing pretty good though so far.

  • 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-09T14:10:07+00:00Added an answer on June 9, 2026 at 2:10 pm

    It works for me.

    What ruby version are you using (ruby -v).

    My code (with ruby 1.9.3):

    $ irb
    1.9.3-p125 :001 > a = [ "a", "b", "c" , "d"]  => ["a", "b", "c", "d"] 
    1.9.3-p125 :002 > a.index("d")  => 3
    

    To get the ‘next’ element, e.g. to get the next 1 element after ‘b’:

    a[a.index("b")+1]
    
    => "c" 
    

    Of oucrse “c” is at position 2 (zero based numbering for arrays) as you can see with

    a.index(a[a.index("b")+1])
    => 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I have fixed all but two warnings now, so thank you all for
EDIT: Server is MOSS 2007 Enterprise, running SP1 and all patches up to, but
EDIT: Fixed most of the problem (but not too sure why). Check the bottom
Edit: Now fixed! Thank you for your help! Sorry for the beginner questions, I'm
I am working on a feature to edit all the items in the table.
I have this function to edit all fields that come from the form and
I am using a nested model form to edit all phone numbers of a
EDIT: I where provided several interfaces that describe all the functionality that the project
*edit: I understand that stackoverflow has banned all questions regarding using Microsoft's Visio. I'm
EDIT I've just started skimming Codd's famous 1970 paper that started it all, that

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.