repeat a method inside a string

Discussion in 'Ruby' started by Zoe Phoenix, Jul 19, 2008.

  1. Zoe Phoenix

    Zoe Phoenix Guest

    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/.
     
    Zoe Phoenix, Jul 19, 2008
    #1
    1. Advertisements

  2. On Sat, Jul 19, 2008 at 3:44 PM, Zoe Phoenix
    <> 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
     
    Michael Guterl, Jul 19, 2008
    #2
    1. Advertisements

  3. Zoe Phoenix

    Zoe Phoenix Guest

    Michael Guterl wrote:
    > On Sat, Jul 19, 2008 at 3:44 PM, Zoe Phoenix
    > <> 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/.
     
    Zoe Phoenix, Jul 19, 2008
    #3
  4. Zoe Phoenix

    Tachikoma Guest

    On Jul 20, 3:44 am, Zoe Phoenix <> 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
     
    Tachikoma, Jul 20, 2008
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frank

    Repeat DataGrid?

    frank, Jul 11, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    547
    frank
    Jul 11, 2003
  2. Shawn
    Replies:
    2
    Views:
    15,974
    Furious George
    Sep 29, 2006
  3. ankur
    Replies:
    1
    Views:
    14,348
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  4. tobleron
    Replies:
    6
    Views:
    498
  5. arnuld

    Finding a string inside string

    arnuld, Nov 23, 2010, in forum: C Programming
    Replies:
    3
    Views:
    496
    Dann Corbit
    Nov 23, 2010
  6. Just Another Victim of the Ambient Morality

    How to repeat string patterns in Ruby?

    Just Another Victim of the Ambient Morality, Jun 19, 2007, in forum: Ruby
    Replies:
    7
    Views:
    234
    Just Another Victim of the Ambient Morality
    Jul 11, 2007
  7. Kyung won Cheon
    Replies:
    0
    Views:
    386
    Kyung won Cheon
    Nov 21, 2008
  8. Csaba  Gabor
    Replies:
    8
    Views:
    257
    Richard Cornford
    May 10, 2006
Loading...