# Repeat a method inside a string

**URL:** <https://www.ruby-forum.com/t/repeat-a-method-inside-a-string/143515>\
**Category:** Ruby\
**Created:** [July 19, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/repeat-a-method-inside-a-string/143515 "2008-07-19T21:49:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [July 19, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/repeat-a-method-inside-a-string/143515/1 "2008-07-19T21:49:18Z")

</div>

I have a method that will generate a random HTML link, but I need to  
have it repeat inside a string interpolation. I need to generate a  
number of random links at the bottom of HTML pages that are being  
generated. I know how to insert the method into the string so it  
inserts a random link there, but I don’t know how to get it to insert 30  
different links instead of just one.

Inside the HTML, it looks like this to just insert one link:

########  
…

Sitemap

  
 #{insertlink}   

... ########

What I don’t know how to do is to get it to do the insertlink method 30  
times in that spot instead of just once. Help, please…?

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [July 19, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/repeat-a-method-inside-a-string/143515/2 "2008-07-19T22:05:24Z")

</div>

On Sat, Jul 19, 2008 at 3:44 PM, Zoe P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> …  
> times in that spot instead of just once. Help, please…?  
> Why not just write a method that calls the other method 30 times and  
> collects the results in an array, then joins them to a string?

def foo  
r = []  
30.times do  
r \<\< insertlink  
end  
r.join(’ ')  
end

HTH,  
Michael G.

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [July 19, 2008, 11:33pm UTC](https://www.ruby-forum.com/t/repeat-a-method-inside-a-string/143515/3 "2008-07-19T23:33:42Z")

</div>

Michael G. wrote:

> On Sat, Jul 19, 2008 at 3:44 PM, Zoe P.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > …  
> > times in that spot instead of just once. Help, please…?  
> > Why not just write a method that calls the other method 30 times and  
> > collects the results in an array, then joins them to a string?
> 
> def foo  
> r = []  
> 30.times do  
> r \<\< insertlink  
> end  
> r.join(’ ')  
> end
> 
> HTH,  
> Michael G.

That would probably make the most sense… but, since I’m still a fetus  
programmer (I’m not even a baby yet \>.\>; ), I didn’t think of doing  
that. :-p Many thanks!

---

<div class="post-metadata">

**Author:** ![zoephoenix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/45deac/32.png) [@zoephoenix](https://www.ruby-forum.com/u/zoephoenix)\
**Post date:** [July 20, 2008, 4:00am UTC](https://www.ruby-forum.com/t/repeat-a-method-inside-a-string/143515/4 "2008-07-20T04:00:32Z")

</div>

On Jul 20, 3:44 am, Zoe P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## … times in that spot instead of just once. Help, please…?
> 
> Posted viahttp://www.ruby-forum.com/.

you may use  
string \* 30  
to repeat the string 30 times
