String.center

Signature

string.center(length)                 #=> new_string
string.center(length, padding_string) #=> new_string

The String.center method is in the same family of methods as
String.ljust
and
String.rjust.

string.ljust(length) centers string to the given length. If length is
greater than the length of string then a new string is returned that is both
left and right padded to length. If length is less than or equal to the
length of string, then string.center(length) returns a new string that is
equal to string and no padding or truncating occurs.

Examples

'foo'.center(10)      #=> "   foo    "
'foo'.center(10,'x')  #=> "xxxfooxxxx"
'foo'.center(2)       #=> "foo"
'foo'.center(2,'x')   #=> "foo"

l = 'left'            #=> "left"
r = 'right'           #=> "right"
c = 'center'          #=> "center"
l.ljust(8) + c.center(8) + r.rjust(8)   #=> "left     center    right"

text = 

Documentation Reference

www.ruby-doc.org : String.center