Formatting text: receipts, tables, letters
A program that works something out is half the job – the other half is writing the result down nicely. For columns there are ljust (pad on the right, so the text sits left), rjust (pad on the left) and center: you say how wide the field should be, and Ruby fills it with spaces – or with a character of your choice:
puts "bacon".ljust(10) + "4.50".rjust(6)
puts "egg".ljust(10) + "1.20".rjust(6)
"Chunky".center(20, "~")For numbers, format is the tool: a template text with placeholders that start with %. %d is a whole number, %s a string, %.2f a decimal with two digits after the point. A number in between sets the width, a minus aligns to the left:
price = 4.5
puts format("%.2f francs", price)
puts format("%-10s %6.2f", "bacon", price)
format("%03d", 7)The same is shorter with the % operator after a string – several values go in square brackets. And * repeats a string, handy for dividing lines:
puts "-" * 20
puts "%s has %d strips" % ["Chunky", 3]
"=" * 20Longer texts over several lines are written as a heredoc: <<~NAME opens it, a line with NAME ends it. The tilde removes the shared indentation, and #{} works as in any string:
name = "Chunky"
letter = <<~LETTER
Dear #{name},
thanks for the bacon!
Yours, Kaz
LETTER
puts letterText that comes from people is untidy: spaces at the edges, double spaces, a line break at the end. strip cleans the edges, squeeze folds repetitions, and split cuts at a separator into an array:
input = " bacon with egg \n"
p input.strip
p input.strip.squeeze(" ")
"a,b,,c".split(",")All of it together makes a receipt. The hash from lesson 11 (hashes) holds items and prices, each walks through them, and every line is aligned with ljust and format:
order = { "bacon" => 4.5, "egg" => 1.2, "coffee" => 3.0 }
puts "Receipt".center(18, "=")
order.each do |item, price|
puts item.ljust(12) + format("%6.2f", price)
end
puts "-" * 18
puts "Total".ljust(12) + format("%6.2f", order.values.sum)Remember: ljust, rjust and center for columns, format for numbers, heredocs for paragraphs, strip and split for tidying up. The documentation lists more placeholders for format – %x for hexadecimal, %e for powers of ten, %% for a real percent sign.
shopping into a shopping list list: a string with one line per item – the name on the left, padded to 10 characters, then the price with two decimals, right-aligned on 7 characters (Bacon 4.50). The lines are joined with "\n", with no line break at the end.shopping = { "Bacon" => 4.5, "Bread" => 2, "Honey" => 6.8 }
# list = ... # one line per item: name on 10 characters, price on 7 with two decimals
table: first a header line with Item on the left, padded to 12 characters, and Price right-aligned on 6; then a line of 18 dashes; then one line per item like in the receipt above (name on 12, price with %6.2f). The lines are joined with "\n", no line break at the end.shopping = { "Bacon" => 4.5, "Bread" => 2, "Honey" => 6.8 }
# table = ... # a header line "Item" / "Price", a line of 18 dashes, then one line per item