From: Robert Dober Date: 2008-07-18T17:00:43+09:00 Subject: Re: inserting a random item from an array On Fri, Jul 18, 2008 at 7:05 AM, Zoe Phoenix wrote: > I'm trying to have Ruby generate a random list of 30 links to put at the > bottom of a web page and write it to a file. I think I'm on the right > track of how to do it, but I'm not sure... > > ############## > citylist = ['Orlando', > 'Tampa', > 'Fort Lauderdale', > 'Miami', > 'St. Petersburg', > 'Etc.' > ] > > output = > 30.times do [rand(citylist.length)] > %Q( href="http://www.website.com/cities/website#{rand(citylist.length)}.html">Website > #{rand(citylist.length)} | ) > end > > File::open("I:\\My Documents\\Stuff\\More Stuff\\websitelinks.txt", 'w') > do |f| > f.write output > end > > ############## > > I may be mistaken about the use of the block here... Someone please tell > me what I'm doing wrong and how to fix it? The two instances of the > city inside the block should be the same, but I'm not sure how to do > that. Doesn't rand make it a random city each time it's interpreted or > since it's in a block, would it be the same each time the block > repeats..? > -- > Posted via http://www.ruby-forum.com/. > > Hmm I would just do this File::open(...,"w") do | f | f.puts citylist.sort_by{ rand }[0...30]. map { | florida_agglomoration | %{a href="http:....#{florida_agglomoration} etc.etc. } } end Your above code created potential doubletons, did not assure the consistency between links and the text. HTH Robert -- http://ruby-smalltalk.blogspot.com/ --- AALST (n.) One who changes his name to be further to the front D.Adams; The Meaning of LIFF