truncate
truncate([length = 30[, suffix = '...']]) -> string
Truncates a string to the given length and appends a suffix to it (indicating that it is only an excerpt).
Of course, String#truncate
does not modify strings which are shorter than the specified length.
If unspecified, the length parameter defaults to 30 and the suffix to "..."
.
Note that String#truncate
takes into consideration the length of the appended suffix so as to make the returned string of exactly the specified length.
Examples
'A random sentence whose length exceeds 30 characters.'.truncate();
// -> 'A random sentence whose len...'
'Some random text'.truncate();
// -> 'Some random text.'
'Some random text'.truncate(10);
// -> 'Some ra...'
'Some random text'.truncate(10, ' [...]');
// -> 'Some [...]'