Home Segments Index Top Previous Next

491: Mainline

Of course, the version of formatWithWidth: defined in Segment 490 crashes in the event the prescribed width is less than the length of the given string. Accordingly, you should test the length of the given string, and, if it is too big, produce a string of the prescribed length filled with * characters. Filling the field with * characters is the standard way of informing human readers that you made an attempt to squeeze a too-large item into a too-narrow space:

String method definition • instance 
formatWithWidth: w 
  self size <= w 
    ifTrue: [^ ((String new: w - self size) atAllPut: $ )  , self] 
    ifFalse: [^ (String new: w) atAllPut: $*]