0

I have a backup script, which I take all the objects in a directory, and then with each slice of 30,000 I back them up to S3. My questions is now that I have over 100,000 objects, I would like to skip to slice number 2 but I am unsure how to do that. So the beginning of the loop looks like -

directory.files.each_slice(30000) do |file_array|

directory.files.each_slice(30000).skip(1) 

Any thoughts?

Thanks!

megas
  • 21,401
  • 12
  • 79
  • 130
tspore
  • 1,349
  • 2
  • 11
  • 14

3 Answers3

2

each_slice returns an enumerable which you can then call further enumerable methods on, so you could use with_index to do something like

directory.files.each_slice(30000).with_index { | file_array, i | 
    next if i == 2 
    upload file_array 
}
Oliver Atkinson
  • 7,970
  • 32
  • 43
1

Your hypothetical skip method is called drop:

directory.files.each_slice(30000).drop(1).each do |file_array|

Note that like all Enumerable methods, it is not type-preserving, it always returns an Array, even though you are calling it on an Enumerator. Since you only have about 4 slices at the moment, that's not going to be a problem, but if you had millions of slices, that would eat up your memory pretty quickly.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
0

How about this:

directory.files.each_slice(30000).with_index do |files, idx| 
  next if idx == 0
  ...
end
ShadyKiller
  • 700
  • 8
  • 16