Using n.times with gsub

GOAL: one-liner substitute of 5 spaces at beginning of a line.

WORKS: gsub(/^/, " ")
FAILS: gsub(/^/, 5.times {putc " "})

-example-
WORKS: $ cat foo.out | ruby -pe 'gsub(/^/, " ")'
FAILS: $ cat foo.out | ruby -pe 'gsub(/^/, 5.times{putc " "})'

I've tried variations of putc, puts, and print. All fail in different
ways.

thoughts? -- dave

cat foo.out | ruby -pe 'gsub(/^/, " " * 5)
cat foo.out | ruby -pe 'gsub(/^/, (1..5).collect { " " }.join }

···

On Monday 14 November 2005 09:42 pm, davidpthomas@gmail.com wrote:

GOAL: one-liner substitute of 5 spaces at beginning of a line.

WORKS: gsub(/^/, " ")
FAILS: gsub(/^/, 5.times {putc " "})

-example-
WORKS: $ cat foo.out | ruby -pe 'gsub(/^/, " ")'
FAILS: $ cat foo.out | ruby -pe 'gsub(/^/, 5.times{putc " "})'

I've tried variations of putc, puts, and print. All fail in different
ways.

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

n.times returns n, so gsub, expecting a string instead of a number,
croaks at that.

I'm not sure if calling output functions inside a substitution is
something you really want to do... wouldn't something like Jim's
answer, or even " " * 5 make more sense?

Sam

···

On 11/15/05, davidpthomas@gmail.com <davidpthomas@gmail.com> wrote:

WORKS: $ cat foo.out | ruby -pe 'gsub(/^/, " ")'
FAILS: $ cat foo.out | ruby -pe 'gsub(/^/, 5.times{putc " "})'

works:

   harp:~ > cat a
   a
   b
   c
   harp:~ > ruby -pe ' 5.times{ gsub /^/, 32.chr } ' < a
        a
        b
        c

simpler:

   harp:~ > ruby -pe ' sub /^/, 32.chr * 5' < a
        a
        b
        c

cheers.

-a

···

On Tue, 15 Nov 2005 davidpthomas@gmail.com wrote:

GOAL: one-liner substitute of 5 spaces at beginning of a line.

WORKS: gsub(/^/, " ")
FAILS: gsub(/^/, 5.times {putc " "})

-example-
WORKS: $ cat foo.out | ruby -pe 'gsub(/^/, " ")'
FAILS: $ cat foo.out | ruby -pe 'gsub(/^/, 5.times{putc " "})'

I've tried variations of putc, puts, and print. All fail in different
ways.

thoughts? -- dave

--

ara [dot] t [dot] howard [at] gmail [dot] com
all happiness comes from the desire for others to be happy. all misery
comes from the desire for oneself to be happy.
-- bodhicaryavatara

===============================================================================

Hi,

At Tue, 15 Nov 2005 11:42:17 +0900,
davidpthomas@gmail.com wrote in [ruby-talk:165800]:

GOAL: one-liner substitute of 5 spaces at beginning of a line.

ruby -pe 'print " "' < foo.out

···

--
Nobu Nakada

"asdf".gsub(/^/," "*5)

···

On 11/14/05, Jim Weirich <jim@weirichhouse.org> wrote:

On Monday 14 November 2005 09:42 pm, davidpthomas@gmail.com wrote:
> GOAL: one-liner substitute of 5 spaces at beginning of a line.
>
> WORKS: gsub(/^/, " ")
> FAILS: gsub(/^/, 5.times {putc " "})
>
> -example-
> WORKS: $ cat foo.out | ruby -pe 'gsub(/^/, " ")'
> FAILS: $ cat foo.out | ruby -pe 'gsub(/^/, 5.times{putc " "})'
>
>
> I've tried variations of putc, puts, and print. All fail in different
> ways.

cat foo.out | ruby -pe 'gsub(/^/, " " * 5)
cat foo.out | ruby -pe 'gsub(/^/, (1..5).collect { " " }.join }

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

Hi --

···

On Tue, 15 Nov 2005, Ara.T.Howard wrote:

On Tue, 15 Nov 2005 davidpthomas@gmail.com wrote:

GOAL: one-liner substitute of 5 spaces at beginning of a line.

WORKS: gsub(/^/, " ")
FAILS: gsub(/^/, 5.times {putc " "})

-example-
WORKS: $ cat foo.out | ruby -pe 'gsub(/^/, " ")'
FAILS: $ cat foo.out | ruby -pe 'gsub(/^/, 5.times{putc " "})'

I've tried variations of putc, puts, and print. All fail in different
ways.

thoughts? -- dave

works:

harp:~ > cat a
a
b
c
harp:~ > ruby -pe ' 5.times{ gsub /^/, 32.chr } ' < a
      a
      b
      c

simpler:

harp:~ > ruby -pe ' sub /^/, 32.chr * 5' < a
      a
      b
      c

Tiny further simplification: s/<// :slight_smile:

David

--
David A. Black
dblack@wobblini.net