How to know byte size of a very large string

Hi
I tried
(“aaa”*300000000000000000000000000000000000000000000000000000000000000000000000000000000000000).bytesize
But this gives me error
RangeError: bignum too big to convert into `long’

    Why this? Mine is Ruby1.8.7

Thanks

It looks like for string multiplication, ruby can’t use Bignums.
Bignums are represented internally as strings of digits, and when doing
Bignum * Bignum ruby has to do the math one string digit by one string
digit at a time. Apparently, with String * Bignum ruby doesn’t know
what to do with ‘aaa’ * ‘3’, so ruby tries to convert ‘3’ to an actual
number.

Tom M. wrote in post #1007073:

Hi
I tried

(“aaa”*300000000000000000000000000000000000000000000000000000000000000000000000000000000000000).bytesize

But this gives me error
RangeError: bignum too big to convert into `long’

Do you really have that much memory on your machine? Because you just
asked Ruby to build a string that size (filling it with 'a’s), and then
count the bytes in it.

A ‘long’ is defined by the C standard to be at least 232 (+/- 2GB if
signed), but is 2
64 on many machines. That’s a lot of memory :slight_smile: