# Indenting strings - elegant alternatives?

**URL:** <https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979>\
**Category:** Ruby\
**Created:** [September 19, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979 "2007-09-19T13:15:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [September 19, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979/1 "2007-09-19T13:15:36Z")

</div>

Just in case someone would like to contribute ideas of elegant code:

PROBLEM:

Given a string of lines (for example, “abc\nde\nfgh\n”) and a number n,  
produce a string of the same lines, but each line indented by n spaces.  
If the original string does NOT end in \n, it is OK (but not required)  
to have it ending in \n in the result.

Speed is not an issue; I’m mainly looking EITHER for compact/elegant  
code in Core Ruby (noing well that elegance is a very subjective  
criterium), OR for some library function which already has solved this.

Here is my own (pretty straightforward) solution to the problem:

def indent(n,s)  
(s.split.map {|x| (’ '\*n)+x}.join("\n"))+"\n"  
end

This works, so the only reason I’m posting this, is to learn about  
alternative ways of doing this.

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [September 19, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979/2 "2007-09-19T13:42:23Z")

</div>

Ronald F. wrote:

> Given a string of lines (for example, “abc\nde\nfgh\n”) and a number n,  
> produce a string of the same lines, but each line indented by n spaces.

This would be my first inclination:  
" “\*n + s.gsub(”\n", “\n”+" "\*n)

BTW, I don’t think yours handles “abc\n\ndef” as you might hope…

best,  
Dan

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [September 19, 2007, 1:47pm UTC](https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979/3 "2007-09-19T13:47:47Z")

</div>

On Sep 19, 2007, at 1:13 PM, Ronald F. wrote:

> Speed is not an issue; I’m mainly looking EITHER for compact/elegant  
> code in Core Ruby (noing well that elegance is a very subjective  
> criterium), OR for some library function which already has solved  
> this.
> 
> Here is my own (pretty straightforward) solution to the problem:
> 
> def indent(n,s)  
> (s.split.map {|x| (’ '\*n)+x}.join("\n"))+"\n"  
> end

Another solution is:

s.gsub(/^/, ’ ’ \* n)

– fxn

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [September 19, 2007, 2:05pm UTC](https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979/4 "2007-09-19T14:05:52Z")

</div>

> BTW, I don’t think yours handles “abc\n\ndef” as you might hope…

Indeed! Thank you for pointing this out!

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [September 19, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979/5 "2007-09-19T14:01:50Z")

</div>

On Sep 19, 6:13 am, “Ronald F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> code in Core Ruby (noing well that elegance is a very subjective  
> criterium), OR for some library function which already has solved this.
> 
> Here is my own (pretty straightforward) solution to the problem:
> 
> def indent(n,s)  
> (s.split.map {|x| (’ '\*n)+x}.join(“\n”))+“\n”  
> end
> 
> This works, so the only reason I’m posting this, is to learn about  
> alternative ways of doing this.

def indent(n,s)  
s.gsub( /^/, ’ '\*n )  
end

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [September 20, 2007, 7:16am UTC](https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979/6 "2007-09-20T07:16:10Z")

</div>

something like this?  
str.gsub(/\b(\w+)/) {|word| ’ '\*n + word}

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [September 20, 2007, 10:47am UTC](https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979/7 "2007-09-20T10:47:33Z")

</div>

Hi,

Am Donnerstag, 20. Sep 2007, 14:15:11 +0900 schrieb Federico  
Zagarzazú:\> something like this?

> str.gsub(/\b(\w+)/) {|word| ’ '\*n + word}

/\b/ always matches before and after /\w+/.

Bertram

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [September 19, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/indenting-strings-elegant-alternatives/114979/8 "2007-09-19T15:29:13Z")

</div>

Hi,

Am Mittwoch, 19. Sep 2007, 20:46:30 +0900 schrieb Xavier N.:

> Another solution is:
> 
> s.gsub(/^/, ’ ’ \* n)

Or even

```
s.gsub /^(?!$)/, " "*n

```

Bertram
