Read every 8 bytes from a file in Ruby
2 answers
If you are putting the results into an array, you probably have enough memory to read the entire file into a string, in which case you can do the following.
text = "My dog has fleas. Oh, my!"
File.write('temp', text) #=> 26
text = File.read('temp')
((text.size+7)/8).times.map { |i| text.slice(i*8,8) }
#=> ["My dog h", "as fleas", ". Oh, m", "y!"]
or, if you like:
((text.size+7)/8).times.map { text.slice!(0,8) }
0
source to share