From: Josh Cheek Date: 2010-03-28T04:23:43+09:00 Subject: Re: remove overall indentation preserving reletive indentation --001636b2ac50b31f2c0482cd3717 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Mar 27, 2010 at 10:33 AM, Jesse B. wrote: > I am trying to find a solution to remove the overall indentation of a > block of text while preserving the relative indentation of each line, > for example > > line 1 > line 2 > > (2 spaces then 4 spaces) > should become > > line 1 > line 2 > > (0 spaces then two spaces) > > so that the two space between lines relative indentation is preserved, > yet the first two spaces overall indentation goes away. > > what I have tried so far is string .strip, which seems to remove all > leading space of the first line only > > content = > " line 1 > line2" > output = content.strip > print output > > produces > > line 1 > line 2 > > 0 spaces then 4. > > whereas Im going for > > a result of 0 then 2 from starting with 2 then 4. > > I assume the solution would probably involve getting the number of > spaces for the first line and removing that number of spaces from each > line in the block? > > thanks in advance for your help. > -- > Posted via http://www.ruby-forum.com/. > > Perhaps def relative_indented(str) leading_whitespace = str[/\A\s*/] str.gsub /^#{leading_whitespace}/ , String.new end puts relative_indented <