Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

String#ljust

str.ljust(integer, padstr=' ') → new_str

If integer is greater than the length of str, returns a new String of length integer with str left justified and padded with padstr; otherwise, returns str.

Example
"hello".ljust(4)            #=> "hello"
"hello".ljust(20)           #=> "hello               "
"hello".ljust(20, '1234')   #=> "hello123412341234123"

Ruby version changes

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