Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   repeat a method inside a string (http://www.velocityreviews.com/forums/t851679-repeat-a-method-inside-a-string.html)

Zoe Phoenix 07-19-2008 07:44 PM

repeat a method inside a string
 
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..?
--
Posted via http://www.ruby-forum.com/.


Michael Guterl 07-19-2008 07:59 PM

Re: repeat a method inside a string
 
On Sat, Jul 19, 2008 at 3:44 PM, Zoe Phoenix
<dark_sgtphoenix@hotmail.com> wrote:
> 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


Zoe Phoenix 07-19-2008 09:28 PM

Re: repeat a method inside a string
 
Michael Guterl wrote:
> On Sat, Jul 19, 2008 at 3:44 PM, Zoe Phoenix
> <dark_sgtphoenix@hotmail.com> 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 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!
--
Posted via http://www.ruby-forum.com/.


Tachikoma 07-20-2008 01:57 AM

Re: repeat a method inside a string
 
On Jul 20, 3:44*am, Zoe Phoenix <dark_sgtphoe...@hotmail.com> wrote:
> 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..?
> --
> Posted viahttp://www.ruby-forum.com/.


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


All times are GMT. The time now is 02:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.