Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

String#ljust

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

Returns a copy of self, left-justified and, if necessary, right-padded with the pad_string:

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

If width <= self.length, returns a copy of self:

Example
'hello'.ljust(5)  # => "hello"
'hello'.ljust(1)  # => "hello"  # Does not truncate to width.

Related: see Converting to New String.

Ruby version changes

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