# Remove overall indentation preserving reletive indentation

**URL:** <https://www.ruby-forum.com/t/remove-overall-indentation-preserving-reletive-indentation/185860>\
**Category:** Ruby\
**Created:** [March 27, 2010, 5:33pm UTC](https://www.ruby-forum.com/t/remove-overall-indentation-preserving-reletive-indentation/185860 "2010-03-27T17:33:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xtremebain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/a6a055/32.png) [@xtremebain](https://www.ruby-forum.com/u/xtremebain)\
**Post date:** [March 27, 2010, 5:33pm UTC](https://www.ruby-forum.com/t/remove-overall-indentation-preserving-reletive-indentation/185860/1 "2010-03-27T17:33:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![xtremebain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/a6a055/32.png) [@xtremebain](https://www.ruby-forum.com/u/xtremebain)\
**Post date:** [March 27, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/remove-overall-indentation-preserving-reletive-indentation/185860/2 "2010-03-27T18:00:10Z")

</div>

Hello Jesse,

> 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?

You are also assuming your text is consistently indented from the  
first line, that is you don’t have

…line 1  
…line 2  
…line 3

for example. A better way would be (I think) to check first each line  
to search for the minimum number of spaces and then remove that number  
of spaces from each line (assuming also that you have normal spaces  
and no tabulations for example). One way would be

string = " line1\n line2\n line3"  
puts string  
arr = string.split(/\n/)  
nb = arr.collect {|line| line =~ /^( \*)/ ; $1.length}.min  
new\_string = arr.collect {|line| line.sub(/ {#{nb}}/,’’)}.join("\n")  
puts new\_string

Cheers,

---

<div class="post-metadata">

**Author:** ![xtremebain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/a6a055/32.png) [@xtremebain](https://www.ruby-forum.com/u/xtremebain)\
**Post date:** [March 27, 2010, 8:24pm UTC](https://www.ruby-forum.com/t/remove-overall-indentation-preserving-reletive-indentation/185860/3 "2010-03-27T20:24:08Z")

</div>

On Sat, Mar 27, 2010 at 10:33 AM, Jesse B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## line 1 content = 0 spaces then 4.
> 
> Posted via [http://www.ruby-forum.com/](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 \<\<END\_OF\_TEXT  
Two Spaces  
Four Spaces  
Six Spaces  
Four Spaces  
Two Spaces  
END\_OF\_TEXT

A few things, though. Everything is relative to your initial indenting,  
so  
if you have a line later with less indenting, then it will just go to  
zero  
(will not be relative, because it would need negative indenting). You  
could  
get around this by checking each line to find the one with the largest  
indenting, and make them relative to that, whereas I have just checked  
it  
against the first line in the string. Also, mixing spaces with tabs will  
mess it up, unless you are consistent with your use across every line.
