Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby IO#read max length for single read

Tags:

io

ruby

How can i determine the max length IO#read can get in a single read on the current platform?

irb(main):301:0> File.size('C:/large.file') / 1024 / 1024
=> 2145
irb(main):302:0> s = IO.read 'C:/large.file'
IOError: file too big for single read
like image 381
jmd Avatar asked Apr 26 '26 01:04

jmd


1 Answers

That message comes from io.c, remain_size. It is emitted when the (remaining) size of the file is greater or equal to LONG_MAX. That value depends on the platform your Ruby has been compiled with.

At least in Ruby 1.8.7, the maximum value for Fixnums happens to be just half of that value (-1), so you could get the limit by

2 * 2 ** (1..128).to_a.find { | i | (1 << i).kind_of? Bignum } - 1

You should rather not rely on that.

like image 180
undur_gongor Avatar answered Apr 28 '26 15:04

undur_gongor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!