Darragh Darragh - 2 months ago 10
Ruby Question

Ruby - Passing array variable to method

I'm having trouble passing an array variables to a method in rails. I'm using the flickraw gem and want to link a user's NSID to their username but it's returning with the error,


"'flickr.people.getInfo' - User not found"


I've output the variables to the cli and tested each one individually. Can see each NSID so can't understand why I'm getting the error.

Here's my code

require "flickraw"

class PhotosController < ApplicationController

def index
@list = flickr.photos.getRecent(:per_page => '3')
@list.each do |info|
store = [info.owner]
for i in 0..store.length
person = flickr.people.getInfo(:user_id => store[i])
puts person.username
end
end
end
end


Here's the links to Flickraw and Flickr-api docs,
Flickraw docs, flickr-api docs

Any help is always appreciated.

Answer

The problem is here:

for i in 0..store.length

this range is inclusive. You should use exclusive range (note three dots):

for i in 0...store.length

For inclusive range, there appear to be length + 1 iterations, and for the last one the respective array has no index, returns nil and everything fails.


Another option would be to use the inclusive range till length - 1:

for i in 0..store.length-1
Comments