liveBook · Manning

Pack

As explained in The Well-Grounded Rubyist, Fourth Edition

pack is a Ruby Array method that converts array elements into a string according to a format directive.

Ruby’s Array#pack converts array elements into a string according to a format directive. With "C*", it treats every element as an unsigned character value and packs all of them into one string—the practical equivalent of converting each value to a character and joining the results. section 10.11.3

Rebuilding a string from XOR-ed bytes

An XOR routine maps over a string’s bytes, XORs each byte with the next byte from a cycling key enumerator, and finishes with .pack("C*"). Applying the same operation twice reconstructs the original string. section 10.11.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains array-to-string conversion and the C* directive through an implemented XOR example, without exhaustive format directives.
Examples
Uses an exclusive-or String#^ method that packs mapped byte values and reverses the transformation with the same key.
What it assumes
Assumes familiarity with Ruby arrays, strings, bytes, map, and enumerators.
What you can do after
Apply pack with C* to turn an array of unsigned character values into a string.
How it connects
Connects arrays, strings, bytes, enumerators, mapping, and XOR operations.

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.