Reason #27 • January 27th, 2026

String padding

Remember the npm left-pad incident from 2016? Good times!

Luckily, with Ruby we've never had to remember how to program because of the standard library methods #ljust and #rjust (short for left-justify and right-justify):

Ruby
fruits_and_prices = {
  "Apple" => 1.2,
  "Banana" => 0.8,
  "Cherry" => 2.5,
  "Dragonfruit" => 5.0,
}

puts "What would you like to order?"

longest_fruit_length = fruits_and_prices.keys.map(&:length).max

fruits_and_prices.each do |fruit, price|
  puts "  #{fruit.ljust(longest_fruit_length + 1)} $#{price}"
end

# Output:
# What would you like to order?
#   Apple        $1.2
#   Banana       $0.8
#   Cherry       $2.5
#   Dragonfruit  $5.0
      
JavaScript
const fruitsAndPrices = {
  Apple: 1.2,
  Banana: 0.8,
  Cherry: 2.5,
  Dragonfruit: 5.0,
};

console.log("What would you like to order?");

const longestFruitLength = Math.max(
  ...Object.keys(fruitsAndPrices).map((fruit) => fruit.length)
);

for (const [fruit, price] of Object.entries(fruitsAndPrices)) {
  console.log(
    `  ${fruit.padEnd(longestFruitLength + 1, " ")} $${price}`
  );
}
// Output:
// What would you like to order?
//   Apple        $1.2
//   Banana       $0.8
//   Cherry       $2.5
//   Dragonfruit  $5.0
      

Apparently, JavaScript attempted to resolve its public image crisis in 2017 by introducing String.prototype.padStart and String.prototype.padEnd. Whether this did anything to improve the npm dependency hell situation I can't tell. But what I can tell you that Ruby has another padding trick up its sleeve: String#center:

Ruby
puts " Welcome to the Fruit Shop ".center(40, "-")

# Output:
# ----------- Welcome to the Fruit Shop -----------
      
JavaScript
const title = " Welcome to the Fruit Shop ";
console.log(
  title.padStart((40 + title.length) / 2, "-").padEnd(40, "-")
);

// Output:
// ----------- Welcome to the Fruit Shop -----------
      

That's more like it!

History

All three methods have been part of Ruby since at least version 1.4. However, it wasn't until Ruby 1.8.0 in 2003 that the optional padding character argument was added.

Here's to hoping we never have to remember how to program! 🥂