Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

help with random links in Ruby

I'm not sure if this is the right forum for this or not... I wasn't sure where else to put it, if anywhere.

Anyway, what I'm trying to do right now is generate a series of web pages from a list of cities in an array using Ruby.  The thing is, I need to have around 30 links in the footer and they need to be random for each page that is generated.  I figured out how to generate random links and they print in the command line, but they don't save to the file like I need them to. -_-

def insertlink

cities = ['Alachua',
'Altamonte Springs',
'Apalachicola',
'Apopka',
'Arcadia',
'Atlantic Beach',
'Auburndale',
'Avon Park',
'Bartow',
'Belle Glade',
'Belleview',
'Beverly Hills',
'Big Pine Key',
'Blountstown',
'Boca Raton',
'Bonifay',
'Bonita Springs',
'Boynton Beach',
'Bradenton',
'Brandon',
'Brooksville',
'Bunnell',
'Bushnell',
'Callahan',
'Cantonment',
'Cape Canaveral',
'Cape Coral',
'Casselberry',
'Chiefland',
'Chipley',
'Clearwater',
'Clermont',
'Clewiston',
'Cocoa',
'Cocoa Beach',
'Crawfordville',
'Crestview',
'Crystal River',
'Dade City',
'Dania',
'Daytona Beach',
'Debary',
'Deerfield Beach',
'Defuniak Springs',
'Deland',
'Delray Beach',
'Deltona',
'Destin',
'Dunedin',
'Dunnellon',
'Eastpoint',
'Edgewater',
'Englewood',
'Estero',
'Eustis',
'Fernandina Beach',
'Flagler Beach',
'Fort Myers',
'Fort Myers Beach',
'Fort Pierce',
'Fort Walton Beach',
'Gainesville',
'Green',
'Cove Springs',
'Groveland',
'Gulf Breeze',
'Haines City',
'Hallandale',
'Havana',
'Hernando',
'High Springs',
'Hobe Sound',
'Holiday',
'Hollywood',
'Etc.'
]

city = cities[rand(cities.length)]

puts %Q{<a hr ef="http://www.website.com/cities/florida/website#{city.delete " "}.html">#{city}</a> | }

end

output = 30.times do
insertlink
end

File::eek:pen("I:\\My Stuff\\Current Desktop Stuff\\generated florida pages\\sitelinks.html", 'w') do |f|
    f.write output
    end

Of course, it's supposed to say "a href" and [/url] isn't supposed to be in there... >.>;;

When the file is created, it just says "30".  I can't figure out how to get it to print the random 30 links to the file... can someone help, please?  I know there's a Ruby person here somewhere that can help meh.
 
1)  Instead of printing the string, which returns nil, use return to pass on the generated string.

2)  output = 30.times do returns 30, not the contents of the iteration.  I'd put the iteration in the file writing block and just print the outcome of insertlink
 
Yeyinde":1s5wdndq said:
1)  Instead of printing the string, which returns nil, use return to pass on the generated string.

2)  output = 30.times do returns 30, not the contents of the iteration.  I'd put the iteration in the file writing block and just print the outcome of insertlink

Can you show me what you mean..? I'm not sure that I follow.  I'm still fairly new at Ruby and don't have an opportunity to study it at length very often.  I guess being self-employed means you have to learn to wear many hats... I'm supposed to have this done hopefully by Monday. >.>;; I could ask someone here to just write the code for me, but I don't have anything I could give in return right now. -_-

I think the problem with putting it in the file writing block would be that I have to be able to insert this toward the bottom of an HTML document ("#HTMLDOCUMENT#" in the code below)... The same city list is going to be used to generate web pages with the variable "city" replaced by each item in the array and saved to a file.  I need 30 random links generated to go at the bottom of each city page that's generated.  So, it should automatically write 30 random links at the bottom of each page as the city pages are generated... do you understand what I mean..? I hope I'm being clear enough. I need to be able to include the random link generator inside the HTML document.

cities = ['Alachua',
'Altamonte Springs',
'Apalachicola',
'Apopka',
'Arcadia',
'Atlantic Beach',
'Auburndale',
'Avon Park',
'Bartow',
'Belle Glade',
'Belleview',
'Beverly Hills',
'Big Pine Key',
'Blountstown',
'Boca Raton',
'Bonifay',
'Bonita Springs',
'Boynton Beach',
'Bradenton',
'Brandon',
'Brooksville',
'Bunnell',
'Bushnell',
'Callahan',
'Cantonment',
'Cape Canaveral',
'Cape Coral',
'Casselberry',
'Chiefland',
'Chipley',
'Clearwater',
'Clermont',
'Clewiston',
'Cocoa',
'Cocoa Beach',
'Crawfordville',
'Crestview',
'Crystal River',
'Dade City',
'Dania',
'Daytona Beach',
'Debary',
'Deerfield Beach',
'Defuniak Springs',
'Deland',
'Delray Beach',
'Deltona',
'Destin',
'Dunedin',
'Dunnellon',
'Eastpoint',
'Edgewater',
'Englewood',
'Estero',
'Eustis',
'Fernandina Beach',
'Flagler Beach',
'Fort Myers',
'Fort Myers Beach',
'Fort Pierce',
'Fort Walton Beach',
'Gainesville',
'Green',
'Cove Springs',
'Groveland',
'Gulf Breeze',
'Haines City',
'Hallandale',
'Havana',
'Hernando',
'High Springs',
'Hobe Sound',
'Holiday',
'Hollywood',
'Etc.'
]

cities.each do |city|
output = %Q(#HTMLDOCUMENT#)

File::eek:pen("I:\\My Stuff\\Current Desktop Stuff\\generated florida pages\\website#{city.delete " "}.html", 'w') do |f|
    f.write output
    end
end
 
A quicky rewrite.

Code:
def insertlink

cities = ['Alachua',
'Altamonte Springs',
'Apalachicola',
'Apopka',
'Arcadia',
'Atlantic Beach',
'Auburndale',
'Avon Park',
'Bartow',
'Belle Glade',
'Belleview',
'Beverly Hills',
'Big Pine Key',
'Blountstown',
'Boca Raton',
'Bonifay',
'Bonita Springs',
'Boynton Beach',
'Bradenton',
'Brandon',
'Brooksville',
'Bunnell',
'Bushnell',
'Callahan',
'Cantonment',
'Cape Canaveral',
'Cape Coral',
'Casselberry',
'Chiefland',
'Chipley',
'Clearwater',
'Clermont',
'Clewiston',
'Cocoa',
'Cocoa Beach',
'Crawfordville',
'Crestview',
'Crystal River',
'Dade City',
'Dania',
'Daytona Beach',
'Debary',
'Deerfield Beach',
'Defuniak Springs',
'Deland',
'Delray Beach',
'Deltona',
'Destin',
'Dunedin',
'Dunnellon',
'Eastpoint',
'Edgewater',
'Englewood',
'Estero',
'Eustis',
'Fernandina Beach',
'Flagler Beach',
'Fort Myers',
'Fort Myers Beach',
'Fort Pierce',
'Fort Walton Beach',
'Gainesville',
'Green',
'Cove Springs',
'Groveland',
'Gulf Breeze',
'Haines City',
'Hallandale',
'Havana',
'Hernando',
'High Springs',
'Hobe Sound',
'Holiday',
'Hollywood',
'Etc.'
]

city = cities[rand(cities.length)]

 return %Q$<a href="http://www.website.com/cities/florida/website#{city.delete(' ')} .html">#{city}</a>$

end


File::open("I:\\My Stuff\\Current Desktop Stuff\\generated florida pages\\sitelinks.html", 'w') do |f|
  30.times {f.puts insertlink}
 end
 
Hmm, why the $ after the %Q?  I know that $ is used in front of a global variable... is that what it's for?

%Q(
...

</div>
<div id="metamorph2"><div id="footer">
  <p><a hr ef="sitemap.html">Sitemap</a></p>
  <br>
  #{insertlink}
  <br>
  </div></div>
<div></div>
<script src="http://www.google -analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2705247-8";
urchinTracker();
</script>
</body>
</html>)

Okay, I put the insertlink method into the HTML document and it generates a random link at the bottom of each page, but the problem is that I need 30 of these instead of just one... and... they need to be a different set of 30 for each page generated. -_-
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top