Ruby: I've seached for ways to remove trailing commas, but have found that : So i'm pretty new to Ruby and programming in general. I'm trying to send data (a list of passwords in a hashlist and how often they appear in the list) in an array to a .txt file in JSON format, which i will then use to make charts. 

Here's the problem: the charts require that the JSON data be in a specific format, like so:
    `[["patches90",35],
    ["Champions17",32],
    ["scotty1977",29],
    ["Kiddos08",25],
    ["holidays2016",21],
    ["scott26me",17],
    ["People1123@",13],
    ["Software0)",9],
    ["Artistic104",5],
    ["bank123",2]]`

with the way i'm currently printing the data, it comes out with an extra trailing comma that i'd like to get rid of. here's what it looks like:

    `[["patches90",35],
     ["Champions17",32],
     ["scotty1977",29],
     ["Kiddos08",25],
     ["holidays2016",21],
     ["scott26me",17],
     ["People1123@",13],
     ["Software0)",9],
     ["Artistic104",5],
     ["bank123",2],`

As you can see, i have an extra comma and a missing closing bracket at the end. I've tried multiple loops and conditions and googled around to find answers with no luck. here's the code i'm using, any help would be appreciated: 

    		f = open('/var/www/html/hashtopussy/top10.txt', 'w')
		f.print "["
		
		@words.sort{|a,b| (a[1]<=>b[1]) * -1}[0, @cap_at].each { |elem|
			percentage = (elem[1].to_f / @total_words_processed) * 100
			ret_str << "#{elem[0]} = #{elem[1].to_s} (#{percentage.round(2).to_s}%)\n"
			
			#my code goes here
			
			f.print  "[#{elem[0].to_json},#{elem[1].to_json}]"
			if (elem != elem.last) 
				f.print ",\n"
			else f.print "]"
			end
			
			#and ends here
		}
		
		#f.print "]\n"
		f.close