# Using n.times with gsub

**URL:** <https://www.ruby-forum.com/t/using-n-times-with-gsub/47919>\
**Category:** Ruby\
**Created:** [November 15, 2005, 3:45am UTC](https://www.ruby-forum.com/t/using-n-times-with-gsub/47919 "2005-11-15T03:45:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidpthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@davidpthomas](https://www.ruby-forum.com/u/davidpthomas)\
**Post date:** [November 15, 2005, 3:45am UTC](https://www.ruby-forum.com/t/using-n-times-with-gsub/47919/1 "2005-11-15T03:45:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidpthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@davidpthomas](https://www.ruby-forum.com/u/davidpthomas)\
**Post date:** [November 15, 2005, 4:00am UTC](https://www.ruby-forum.com/t/using-n-times-with-gsub/47919/2 "2005-11-15T04:00:13Z")

</div>

On Monday 14 November 2005 09:42 pm, removed\_email\_address@domain.invalid wrote:

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

---

<div class="post-metadata">

**Author:** ![davidpthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@davidpthomas](https://www.ruby-forum.com/u/davidpthomas)\
**Post date:** [November 15, 2005, 4:12am UTC](https://www.ruby-forum.com/t/using-n-times-with-gsub/47919/3 "2005-11-15T04:12:15Z")

</div>

On 11/15/05, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

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

---

<div class="post-metadata">

**Author:** ![davidpthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@davidpthomas](https://www.ruby-forum.com/u/davidpthomas)\
**Post date:** [November 15, 2005, 4:15am UTC](https://www.ruby-forum.com/t/using-n-times-with-gsub/47919/4 "2005-11-15T04:15:15Z")

</div>

On Tue, 15 Nov 2005 removed\_email\_address@domain.invalid wrote:

> 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

cheers.

-a

---

<div class="post-metadata">

**Author:** ![davidpthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@davidpthomas](https://www.ruby-forum.com/u/davidpthomas)\
**Post date:** [November 15, 2005, 4:27am UTC](https://www.ruby-forum.com/t/using-n-times-with-gsub/47919/5 "2005-11-15T04:27:17Z")

</div>

Hi –

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

> > 
> 
> b  
> b  
> c

Tiny further simplification: s/\<// 🙂

David

---

<div class="post-metadata">

**Author:** ![davidpthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@davidpthomas](https://www.ruby-forum.com/u/davidpthomas)\
**Post date:** [November 15, 2005, 4:18am UTC](https://www.ruby-forum.com/t/using-n-times-with-gsub/47919/6 "2005-11-15T04:18:16Z")

</div>

“asdf”.gsub(/^/," "\*5)

---

<div class="post-metadata">

**Author:** ![davidpthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb9ed0/32.png) [@davidpthomas](https://www.ruby-forum.com/u/davidpthomas)\
**Post date:** [November 15, 2005, 5:12am UTC](https://www.ruby-forum.com/t/using-n-times-with-gsub/47919/7 "2005-11-15T05:12:22Z")

</div>

Hi,

At Tue, 15 Nov 2005 11:42:17 +0900,  
removed\_email\_address@domain.invalid wrote in [ruby-talk:165800]:

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

ruby -pe ‘print " "’ \< foo.out
