0

I'm developing using Ruby Sinatra and currently using:

gem 'google_drive'
gem 'google-api-client' , '~> 0.9'


@service = Google::Apis::DriveV3::DriveService.new
@service.client_options.application_name = APPLICATION_NAME
@service.authorization = authorize

get_images = @service.list_files(q: "'xxxx' in parents and trashed = false",
  order_by: 'createdTime desc',
  spaces: 'drive',
  fields:'nextPageToken, files(id, name)',
  page_size: 10)

get_images.files.each do |file|
 ......
end

I want to display the images randomly, but

How can I get random image in a folder which contains many images ?

aldrien.h
  • 3,067
  • 2
  • 23
  • 46
  • 4
    Possible duplicate of [How do I pick randomly from an array?](http://stackoverflow.com/questions/3482149/how-do-i-pick-randomly-from-an-array) – DaImTo Jun 02 '16 at 08:34
  • Simply use 'sample': get_images.files.sample, which will return a random file each time. – Sam Jun 02 '16 at 11:44
  • Hi, using 'sample' will select 1 data from array. What if I need 10 random data/images from array? – aldrien.h Jun 03 '16 at 01:52
  • I think I got it. 'shuffle' do the trick get_images.files.shuffle.each do |file| ...end Thanks @DaImTo – aldrien.h Jun 03 '16 at 02:08

0 Answers0