Search code examples
ruby-on-railsrubyhashhashmapinstagram-api

Rails method to iterate through hash of photos and return value according to criteria


I want to create a method (ruby on rails) to iterate through a hash of photos I am pulling from an API (url, name, date created etc) and return only the dates that are in the correct params the user inputs. Inside of the hash you have created_time as one of the keys, and that is what I am interested in. Any suggestions?

def pull_time
  @pictures.map do |picture|
    if picture.created_time >= @start_date && picture.created_time <= @end_date
      return @pictures
    end
  end
end

Solution

  • Use between:

    @pictures.select { |picture| picture.created_time.between?(@start_date, @end_date) }