String#center
str.center(width, padstr = ' ') -> string Returns:
String · Updated March 16, 2026 · String Methods strings padding formatting alignment
The .center method pads a string to a specified width, placing the original string in the center of the padded result. By default, it uses spaces for padding, but you can specify any character or string. This is useful for formatting terminal output, creating ASCII art, or generating aligned text tables.
Syntax
str.center(width) # Pad with spaces (default)
str.center(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 centering with spaces
text = "Ruby"
puts text.center(10)
# Output: " Ruby "
When the total width is larger than the string, Ruby distributes the padding equally on both sides. If the difference is odd, the right side gets one extra character.
puts "Hi".center(10) # " Hi "
puts "Hello".center(10) # " Hello " (extra space on right)
puts "Ruby".center(10) # " Ruby "
Example 2: Using custom padding characters
puts "Ruby".center(10, "*") # "***Ruby***"
puts "Hi".center(10, "-") # "----Hi----"
puts "X".center(5, "=") # "==X=="
You can use multi-character strings for padding:
puts "Yo".center(10, "._.") # "._.Yo._.."
Example 3: Centering in terminal output
title = "Welcome"
puts "+" + "-" * 20 + "+"
puts "|" + title.center(20) + "|"
puts "+" + "-" * 20 + "+"
# Output:
#+--------------------+
#| Welcome |
#+--------------------+
Example 4: Creating ASCII headers
def banner(text, width = 40)
border = "*" * width
puts border
puts text.center(width)
puts border
end
banner("Hello, World!")
# ****************************************
# Hello, World!
# ****************************************
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".center(2) # "Ruby" (no padding needed)
puts "Ruby".center(4) # "Ruby" (exact fit)
Common Patterns
Building formatted tables
headers = ["Name", "Age", "City"]
data = [["Alice", "30", "London"], ["Bob", "25", "Paris"]]
width = 20
puts headers.map { |h| h.center(width) }.join(" | ")
puts "-" * (width * 3 + 4)
data.each do |row|
puts row.map { |cell| cell.center(width) }.join(" | ")
end
# Output:
# Name | Age | City
# ------------------------------------------------------------
# Alice | 30 | London
# Bob | 25 | Paris
Creating centered boxes
def centered_box(text, width = 40)
inner_width = width - 2
padded = text.center(inner_width)
"+" + "-" * inner_width + "+"
"|" + padded + "|"
"+" + "-" * inner_width + "+"
end
puts centered_box("IMPORTANT")
puts centered_box("Click Here")
# +--------------------------------------+
# | IMPORTANT |
# +--------------------------------------+
# | Click Here |
# +--------------------------------------+
Number formatting
numbers = [1, 22, 333, 4444]
numbers.each do |num|
puts num.to_s.center(10, "0")
end
# Output:
# 0000010000
# 0000022000
# 0000333000
# 0004444000
Edge Cases
Empty strings and special characters
puts "".center(5) # " "
puts " ".center(5) # " " (single space becomes 5)
puts "\t".center(5) # tabs also pad correctly
puts "日本語".center(10) # " 日本語 " (Unicode works)
Padding with empty string
puts "Ruby".center(10, "") # Raises ArgumentError: zero width padding
See Also
- String#concat — Concatenate strings together
- String#slice — Extract substrings by index or range
- String#split — Split strings by delimiter