0

How can I change my array's indices to start from 1 instead of 0. I am trying to fetch news from a site (JSON) and after parsing it:

@news = JSON.parse(Net::HTTP.get(URI.parse('http://api.site.com/news?format=json')))

But to see the individual news title, I have to do @news["items"][0] for the first link's title. Is it possible to change that behavior so when I do @news["items"][1] it shows me the first link's title?

8
  • Did I miss something, what's the problem with zero-based arrays? Commented Apr 21, 2012 at 17:09
  • There's nothing wrong with it. It was for a voice application, so when user enters 1 it plays the title for the first link. Now, 0 is pressed for the first link Commented Apr 21, 2012 at 17:10
  • 1
    Every sane language I've tried uses zero-based array access. You better get used to it (or you can switch to VB) :) Commented Apr 21, 2012 at 17:11
  • 2
    Well, you can intercept and interpret that user input, can't you? Commented Apr 21, 2012 at 17:13
  • Bing! Yes, I subtracted 1 from user's input and now it works! Thank you man! Commented Apr 21, 2012 at 17:17

1 Answer 1

3

You should intercept user input and adjust entered value to map to a correct array element. In general, you should always validate user input and check if it makes sense.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.