Talk:Marshaling
From RubySpec
5 minus 5 is not one.
- 84.137.229.38
To clarify 84.137.229.38's comments, the following is from the section Integer Packing:
Unpacking works by reading a single byte as a signed char. (To sign: (n ^ 128) - 128.) Using this byte, you can follow these rules:
- Zero is itself.
- One through four indicate a larger numeral, read n more bytes.
- 5 through 127 represent 1 through 122, respectively. Subtract 5.
- Negative one through four indicate a larger numeral, read n.abs more bytes.
- -5 through -128 represent -1 through -123, respectively. Add 5.
But the third and fifth bullet points aren't quite correct, as 1 is represented by 6 and -1 by -6 (250 in the one byte twos-complement); at least according to my IRB:
irb(main):025:0> Marshal.dump(1)[3] => 6 irb(main):026:0> Marshal.dump(-1)[3] => 250 irb(main):027:0> RUBY_VERSION => "1.8.4"
That leaves the marshal substring "i\005" and "i\373" unaccounted for. Testing them in my irb:
irb(main):038:0> Marshal.load("\004\010i\005")
=> 0
irb(main):039:0> Marshal.load("\004\010i\373")
=> 0
So it looks like 5 through 127 and -5 through -128 follow the rules mentioned in the original text, but there's a typo in the range of the represented values. I'll fix the article now...
- Lukfugl 09:56, 21 November 2006 (CST)

