I would like to know what is the general logic behind the choice of the rib spacing in the thin-walled load bearing structure of a straight or swept all-metal wing? Is it the global or local structural stability, or the skin waveness tolerance, or something else?
All of the above. Mostly it's to achieve conformity to the "mold line", the outer airfoil contour, for as much of the wing as possible, and for buckling resistance of the flattened tube that constitutes a monocoque wing. The buckling resistance mostly means resistance to torsional buckling, the pure bending being absorbed by the main spar.
On a strut braced wing, you can have a single strut and use the skins to make the wing torsionally rigid, or have a strut both fore and aft do provide the torsional rigidity and do away with skins altogether and just cover the wing with fabric.
So you can have more ribs with thinner skins, or less ribs with thicker skins, and it's a juggling act the designer has to work out based on design objectives. For example, the designer may prioritize airfoil conformity between ribs, and use heavier skins that will deform less under air loads, and take advantage of the ability to use fewer ribs to compensate (it's more than just loads - a designer may use thick skins just because they want to use machine countersunk rivets and a minimum thickness is required for them).
On transport airplanes, the upper and lower wing skins are so thick they are called "planks" and actually form the effective upper and lower spar caps of a box structure that spans the entire chord between leading edge and trailing edge, with a relatively small number of ribs to hold the planks apart and provide buckling resistance.
If I'm trying to build a wing as light as possible, I might use more ribs and thin skins to get the torsional rigidity I need and support air loads. Or as mentioned previously, I might brace my wing with lift struts front and rear and use very thin skins that only have to support air loads, or just fabric.