The ruler is made by an expert joiner with an instrument similar to the modern
plane: https://en.wikipedia.org/wiki/Plane_(tool).
The straightness is checked by looking along the edge of the ruler (one end
of the ruler is close to the eye). That is the edge is
compared with the ray of light. In this way, the joiner detects the concavities, of the edge, and carefully removes them, until the edge looks straight.
The surface of the instrument itself does not have to be very straight for this
procedure.
A more difficult question is how the divided ruler is made, if you do not have
a divided ruler for comparison. To divide the ruler, a construction with a compass
is used, essentially consecutive dissections. This is how they divided precision instruments until the second half of 18th century. This was very labor consuming and thus expensive, until a "dividing engine" was invented. It uses essentially a comparison with already existing very finely divided scale.
EDIT. One of the most famous craftsmen of 18 century (John Bird, instrument maker) once said that there is no mechanical way to produce a straight line.
One can only produce an arc of a circle (perhaps of a very great diamter), by compass. Since then, a mechanical mean of producing a straight line was actually discovered (it is called an Inversor, discovered by Paucellier, with rigorous proof by Lipkin
(late 19 century)), https://en.wikipedia.org/wiki/Peaucellier%E2%80%93Lipkin_linkage)
But I've never heard of this device being used in practice.
Its original purpose was to convert the circular motion to rectilinear motion
(and back) in a steam engine. But the progress is lubricants made this invention redundant for its original purpose. It is easier to make a piston to move on
APPROXIMATE straight line, then to implement an inversor.