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
  • 160,959
  • 41
  • 265
  • 366
Ran
  • 3,375
  • 12
  • 43
  • 59

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
  • 200,744
  • 21
  • 223
  • 248
0

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

Guilherme Bernal
  • 8,133
  • 24
  • 40
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