String#rjust
str.rjust(width, padstr = ' ') -> string Returns:
String · Updated March 16, 2026 · String Methods strings padding formatting alignment
The .rjust method pads a string to a specified width on the left side, placing the original string on the right. By default, it uses spaces for padding, but you can specify any character or string. This is useful for formatting numeric output, creating fixed-width tables, or aligning text to the right in terminal applications.
Syntax
str.rjust(width) # Pad with spaces (default)
str.rjust(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 right-justification with spaces
text = "Ruby"
puts text.rjust(10)
# Output: " Ruby"
When the total width is larger than the string, Ruby adds padding to the left side.
puts "Hi".rjust(10) # " Hi"
puts "Hello".rjust(10) # " Hello"
puts "Ruby".rjust(10) # " Ruby"
Example 2: Using custom padding characters
puts "Ruby".rjust(10, "*") # "******Ruby"
puts "Hi".rjust(10, "-") # "--------Hi"
puts "X".rjust(5, "=") # "====X"
You can use multi-character strings for padding:
puts "Yo".rjust(10, "._.") # "._._._._.Yo"
Example 3: Creating formatted numeric tables
numbers = [7, 42, 128, 1024, 4096]
numbers.each do |num|
puts num.to_s.rjust(5)
end
# Output:
# 7
# 42
# 128
# 1024
# 4096
Example 4: Right-aligning variable-width data
labels = ["Name:", "Email:", "Status:"]
values = ["john", "john@example.com", "active"]
labels.zip(values).each do |label, value|
puts "#{value.rjust(20)}: #{label}"
end
# Output:
# john: Name:
# john@example.com: Email:
# active: Status:
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".rjust(2) # "Ruby" (no padding needed)
puts "Ruby".rjust(4) # "Ruby" (exact fit)
Common Patterns
Building CLI tables with right-aligned columns
data = [
["Item", "Qty", "Price"],
["Apple", "5", "2.50"],
["Banana", "12", "1.80"],
["Cherry", "100", "0.50"]
]
# Right-align numeric columns
data.each do |row|
puts row[0].ljust(10) + row[1].rjust(5) + row[2].rjust(10)
end
# Output:
# Item | 5| 2.50
# Banana | 12| 1.80
# Cherry | 100| 0.50
Formatting file sizes
sizes = [512, 1024, 1536, 2048, 4096]
sizes.each do |bytes|
puts "#{bytes.to_s.rjust(6)} bytes"
end
# Output:
# 512 bytes
# 1024 bytes
# 1536 bytes
# 2048 bytes
# 4096 bytes
Creating indented code blocks
code = ["def hello", " puts 'World'", "end"]
code.each do |line|
puts line.rjust(line.length + 4)
end
# Output:
# def hello
# puts 'World'
# end
Edge Cases
Empty strings and special characters
puts "".rjust(5) # " "
puts " ".rjust(5) # " " (single space becomes 5)
puts "\t".rjust(5) # tabs also pad correctly
puts "日本語".rjust(10) # " 日本語" (Unicode works)
Padding with empty string
puts "Ruby".rjust(10, "") # Raises ArgumentError: zero width padding
Negative width
puts "Ruby".rjust(-5) # Raises ArgumentError: negative string size
See Also
- String#ljust — Pad a string on the right (left-justify)
- String#center — Center a string by padding on both sides
- String#slice — Extract substrings by index or range