Ruby’s center Method

Ruby provides a String method to center a String object according to a width, passed in as a parameter.

The width does not include the String object’s length it is called upon, therefore a good trick to center a text is to add the String length plus an even number for the space surrounding it.

text = 'Nicky Larson' + 4)  # => "  Nicky Larson  "

The default padding string is a whitespace (' ') but it can be modified: + 4, '*')  # => "**Nicky Larson**"