From: Joel VanderWerf Date: 2003-01-14T13:16:03+09:00 Subject: Re: RCR: String#indent(n) Gavin Sinclair wrote: > Rubyists, > > I think a fine companion for String#ljust, String#rjust and > String#center would be String#indent. It's pretty easy to just use gsub instead: text.gsub(/^/, " "*4) whereas ljust, rjust, and center would require a little more work to emulate with gsub. Or do you have more functionality in mind? Just for the heck of it, here are some bits I use from time to time. They might fit in some kind of string formatting library. class String # tabs left or right by n chars, using spaces def tab n if n >= 0 gsub(/^/, ' ' * n) else gsub(/^ {0,#{-n}}/, "") end end # preserves relative tabbing # the first non-empty line ends up with n spaces before nonspace def tabto n if self =~ /^( *)\S/ tab(n - $1.length) else self end end # aligns each line def taballto n gsub(/^ */, ' ' * n) end end