[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Indenting strings - elegant alternatives?

Ronald Fischer

9/19/2007 11:13:00 AM

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
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162

7 Answers

Daniel Lucraft

9/19/2007 11:42:00 AM

0

Ronald Fischer 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
--
Posted via http://www.ruby-....

Xavier Noria

9/19/2007 11:47:00 AM

0

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

> 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


William James

9/19/2007 11:47:00 AM

0

On Sep 19, 6:13 am, "Ronald Fischer" <ronald.fisc...@venyon.com>
wrote:
> 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

Ronald Fischer

9/19/2007 12:03:00 PM

0

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

Indeed! Thank you for pointing this out!

Ronald

Bertram Scharpf

9/19/2007 1:27:00 PM

0

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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Federico Zagarzazú

9/20/2007 5:15:00 AM

0

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

On 9/19/07, Ronald Fischer <ronald.fischer@venyon.com> wrote:
> 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
> --
> Ronald Fischer <ronald.fischer@venyon.com>
> Phone: +49-89-452133-162
>
>

Bertram Scharpf

9/20/2007 8:47:00 AM

0

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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...