0

I'm using Marshal.dump to serialize an array of objects, I need to get the size (in KB) of the returned value. Any ideas how to do that?

sawa
  • 165,429
  • 45
  • 277
  • 381
Ran
  • 3,455
  • 12
  • 47
  • 60

3 Answers3

4

Since the output of Marshal.dump is a string, you can just ask for the length of that. The safest way to do this is to ask for bytesize:

dumped = Marshal.dump(array)
kb = dumped.bytesize / 1024

The bytesize method always returns the length of a string in bytes, whereas length returns the length of the string in characters. The two values can differ if you use a multi-byte encoding method like UTF-8.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

What about kbytes = Marshal.dump(ary_of_objs).size / 1000.0?

Guilherme Bernal
  • 8,183
  • 25
  • 43
0
var = Base64.encode64(Marshal.dump(@result))
var.size 

is life saver for me

Ravindra
  • 1,039
  • 2
  • 12
  • 26
  • I know nothing about RoR but this doesn't seem right. http://stackoverflow.com/questions/13378815/base64-length-calculation – spenibus Sep 07 '15 at 14:12