The spines are leaves, and the stem is the leaf
If an areole is a shoot, the spines coming out of it are that shoot's leaves, modified past all recognition. The family gave up on leaves as solar panels because a flat leaf loses water faster than anything else a plant owns, and handed the job of photosynthesis to the green stem instead.
The spines kept working, in several directions at once. They deter animals, obviously. Less obviously, a dense white coat of them shades the stem surface from the worst of the sun and traps a still layer of air against it, and in some species they collect dew and drip it down onto the roots. In the chollas the spines are barbed so a whole joint of the plant detaches onto a passing animal and is carried off to root somewhere else.
The ribs are the other clever piece. A ribbed stem is pleated like a concertina, so after rain it can take up a great deal of water and swell outward by flattening the pleats, then shrink again through a drought, over and over, without ever splitting its own skin.