repeat a method inside a string

Z

Zoe Phoenix

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:

########
...
<div id="site"><div id="footer">
<p><a href="sitemap.html">Sitemap</a></p>
<br>
#{insertlink}
<br>
</div></div><!-- footer ends -->
...
########

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..?
 
M

Michael Guterl

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:

########
...
<div id="site"><div id="footer">
<p><a href="sitemap.html">Sitemap</a></p>
<br>
#{insertlink}
<br>
</div></div><!-- footer ends -->
...
########

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..?

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 Guterl
 
Z

Zoe Phoenix

Michael said:
...
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 Guterl

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!
 
T

Tachikoma

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:

########
..
<div id="site"><div id="footer">
  <p><a href="sitemap.html">Sitemap</a></p>
  <br>
  #{insertlink}
  <br>
  </div></div><!-- footer ends -->
..
########

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..?

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

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top