Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

String#ljust

(::int size, ?::string pad_string) -> ::String
ljust(size, pad_string = ' ') → new_string

Returns a left-justified copy of self.

If integer argument size is greater than the size (in characters) of self, returns a new string of length size that is a copy of self, left justified and padded on the right with pad_string:

Example
'hello'.ljust(10)       # => "hello     "
'  hello'.ljust(10)     # => "  hello   "
'hello'.ljust(10, 'ab') # => "helloababa"
'тест'.ljust(10)        # => "тест      "
'こんにちは'.ljust(10)    # => "こんにちは     "

If size is not greater than the size of self, returns a copy of self:

Example
'hello'.ljust(5)  # => "hello"
'hello'.ljust(1)  # => "hello"

Related: String#rjust, String#center.

Ruby version changes

  1. 3.0
See also #lines #lstrip #length #lstrip! also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0