Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > repeat a method inside a string

Reply
Thread Tools

repeat a method inside a string

 
 
Zoe Phoenix
Guest
Posts: n/a
 
      07-19-2008
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/.

 
Reply With Quote
 
 
 
 
Michael Guterl
Guest
Posts: n/a
 
      07-19-2008
On Sat, Jul 19, 2008 at 3:44 PM, Zoe Phoenix
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Zoe Phoenix
Guest
Posts: n/a
 
      07-19-2008
Michael Guterl wrote:
> On Sat, Jul 19, 2008 at 3:44 PM, Zoe Phoenix
> <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Tachikoma
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 3:44*am, Zoe Phoenix <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method inside method problem, please advise tobleron Java 6 11-26-2008 12:53 PM
Simple error : method format(String, Object[]) is not applicable for the arguments (String, String) ankur Java 1 08-27-2007 06:31 AM
How to repeat string patterns in Ruby? Just Another Victim of the Ambient Morality Ruby 7 07-11-2007 06:21 PM
Why I cannot have a method inside another method? Shawn Java 2 09-29-2006 12:46 AM
returned type of -this- from String.prototype.repeat() Csaba Gabor Javascript 8 05-10-2006 12:10 AM



Advertisments