[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:04077] RE: unpack and "bn" with n>16

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-07-17 11:45:47 UTC
List: ruby-talk #4077
Pixel:
> p "\0\0\0\0".unpack("b16b1b2") # -> ["0000000000000000", "0", "00"]
> p "\0\0\0\0".unpack("b17b1b1") # -> ["00000000000000000", "0", ""]
> 
> I can't find it though :(

String#unpack("bX") where X is bits between 1-8 eat the first byte. When X
is between 9-16 it eats the second byte too (and actually access the first
bits and discards ie. doesn't process the rest).

So "b16" is eating the first two zero bytes, and "b1" the first bit of the
third byte (and consumes the whole bytes), and the "b2" in the end accesses
the last of "\0"s.

Where as the second line consumes first three bytes with "b17", and the last
byte with "b1" and the last "b1" doesn't have any input to eat.

I guess you're expecting the successive "b1b2b3b2" to consume only one byte
whereas it eats the four.

	- Aleksi

In This Thread

Prev Next