String#ljust
str.ljust(width, padstr = ' ') -> string Returns:
String · Updated March 16, 2026 · String Methods strings padding formatting alignment
The .ljust method pads a string to a specified width on the right side, placing the original string on the left. By default, it uses spaces for padding, but you can specify any character or string. This is useful for formatting table columns, creating fixed-width output, or aligning text in terminal applications.
Syntax
str.ljust(width) # Pad with spaces (default)
str.ljust(width, padstr) # Pad with custom string
Parameters
| Parameter | Type | Description |
|---|---|---|
width | Integer | The total width of the resulting padded string |
padstr | String | The string to use for padding (default is space) |
Worked Examples
Example 1: Basic left-justification with spaces
text = "Ruby"
puts text.ljust(10)
# Output: "Ruby "
When the total width is larger than the string, Ruby adds padding to the right side.
puts "Hi".ljust(10) # "Hi "
puts "Hello".ljust(10) # "Hello "
puts "Ruby".ljust(10) # "Ruby "
Example 2: Using custom padding characters
puts "Ruby".ljust(10, "*") # "Ruby******"
puts "Hi".ljust(10, "-") # "Hi--------"
puts "X".ljust(5, "=") # "X===="
You can use multi-character strings for padding:
puts "Yo".ljust(10, "._.") # "Yo._._._._."
Example 3: Creating formatted table columns
data = [
["Name", "Age", "City"],
["Alice", "30", "London"],
["Bob", "25", "Paris"],
["Carolyn", "28", "Berlin"]
]
# Print table with left-justified columns
data.each do |row|
puts row.map { |cell| cell.ljust(10) }.join(" | ")
end
# Output:
# Name | Age | City
# ------------------------------------------------
# Alice | 30 | London
# Bob | 25 | Paris
# Carolyn | 28 | Berlin
Example 4: Aligning variable-width data
labels = ["User:", "Email:", "Status:"]
values = ["john", "john@example.com", "active"]
labels.zip(values).each do |label, value|
puts "#{label.ljust(10)} #{value}"
end
# Output:
# User: john
# Email: john@example.com
# Status: active
Example 5: When width is less than string length
If the specified width is less than or equal to the string’s length, Ruby returns the original string unchanged.
puts "Ruby".ljust(2) # "Ruby" (no padding needed)
puts "Ruby".ljust(4) # "Ruby" (exact fit)
Common Patterns
Building CLI output
def print_key_value(key, value, width = 30)
puts "#{key.ljust(width)}: #{value}"
end
print_key_value("Version", "3.2.0")
print_key_value("Author", "Yukihiro Matsumoto")
print_key_value("License", "BSD-2-Clause")
# Output:
# Version: 3.2.0
# Author: Yukihiro Matsumoto
# License: BSD-2-Clause
Generating fixed-width columns for logs
def log_line(level, message, width = 50)
timestamp = Time.now.strftime("%H:%M:%S")
puts "[#{timestamp}] #{level.ljust(8)} #{message.ljust(width)}"
end
puts log_line("INFO", "Application started")
puts log_line("DEBUG", "Loading configuration file")
puts log_line("ERROR", "Connection timeout after 30s")
puts log_line("WARN", "Deprecated API called")
# Output:
# [18:33:00] INFO Application started
# [18:33:00] DEBUG Loading configuration file
# [18:33:00] ERROR Connection timeout after 30s
# [18:33:00] WARN Deprecated API called
Edge Cases
Empty strings and special characters
puts "".ljust(5) # " "
puts " ".ljust(5) # " " (single space becomes 5)
puts "\t".ljust(5) # tabs also pad correctly
puts "日本語".ljust(10) # "日本語 " (Unicode works)
Padding with empty string
puts "Ruby".ljust(10, "") # Raises ArgumentError: zero width padding
Negative width
puts "Ruby".ljust(-5) # Raises ArgumentError: negative string size
See Also
- String#slice — Extract substrings by index or range
- String#center — Center a string by padding on both sides
- String#lstrip — Remove leading whitespace from a string