Indenting strings - elegant alternatives?

R

Ronald Fischer

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,=20
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=20
code in Core Ruby (noing well that elegance is a very subjective=20
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
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
 
D

Daniel Lucraft

Ronald said:
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
 
X

Xavier Noria

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

Another solution is:

s.gsub(/^/, ' ' * n)

-- fxn
 
W

William James

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.


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

Ronald Fischer

BTW, I don't think yours handles "abc\n\ndef" as you might hope...

Indeed! Thank you for pointing this out!

Ronald
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 19. Sep 2007, 20:46:30 +0900 schrieb Xavier Noria:
Another solution is:

s.gsub(/^/, ' ' * n)

Or even

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

Bertram
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,255
Members
46,856
Latest member
MyronKatz6

Latest Threads

Top