From: Tobias DiPasquale Date: 2002-01-23T21:19:41+09:00 Subject: Re: [POLL]: ruby-talk seperation Hi again, I just wanted to post a little piece of code I just wrote to help me keep track of the poll. It is fairly crude, but it does the job, and I really prefer this method. (since I hate spreadsheets with a passion! Excel turned me off forever!) # program to handle voting for ruby-talk separation poll # by Tobias DiPasquale (not Reif ;-) # class Votes def initialize @storePath = "/home/anany/tmp/poll-store" @votes = {} self.load end def cast(list) if !@votes.has_key?(list) @votes[list] = 1 elsif list == "" break else @votes[list] = @votes[list] + 1 end end def load if File.exist?(@storePath) File.open(@storePath) { |f| @votes = Marshal.load(f) } end end def store File.open(@storePath, "w+") { |f| Marshal.dump(@votes, f) } end def empty? return true if @votes == {} return false end def tally count = 0 puts "List\t\t\tVotes" puts "----\t\t\t-----" @votes.each_pair { |key, value| printf("%s\t\t%s\n", key.chomp, value.to_s) count = count + value } puts "\nTotal Votes: " + count.to_s end def prune if @votes.has_key?("\n") puts "Pruning vote record" @votes.delete("\n") else puts "Nothing to do" end self.store end end #Votes class Names def initialize @storePath = "/home/anany/tmp/names-store" @names = [] self.load end def load if File.exist?(@storePath) File.open(@storePath) { |f| @names = Marshal.load(f) } end end def store File.open(@storePath, "w+") { |f| Marshal.dump(@names, f) } end def addName(name) if !@names.include?(name) return false if name == "\n" @names.push(name) return true else return false end end def count @names.size end end #Names def tallyVotes puts "<<< Ruby-Talk Mailing List Separation Poll Results Monitor >>>" votes = Votes.new names = Names.new votes.tally if !votes.empty? puts "People voted: " + names.count.to_s end def enterVote puts "<<< Ruby-Talk Mailing List Separation Poll Results Monitor >>>" names = Names.new votes = Votes.new printf "Enter name: " name = $stdin.gets if !names.addName(name) error("Person already voted") end for i in 1..5 printf "Enter vote: " vote = $stdin.gets votes.cast(vote) unless vote.empty? end votes.prune names.store end def prune votes = Votes.new votes.prune end def error(msg) puts "\n[ERROR]: " + msg exit(1) end if __FILE__ == $0 if $*.include?("-t") tallyVotes elsif $*.include?("-p") prune else enterVote end end As you can see, names are not associated with votes, and I only put the naming store in there to keep track of who I entered already and who I didn't. I will be using this to keep track of all the votes I find, and have already entered all those from the beginning of these threads to now (7:18AM EST). Comments on the source are welcome (please, do!). Thanks again! -- << T o b i a s D i P a s q u a l e >> anany@ece.villanova.edu | tdipas01@villanova.edu | toby@khenry.com "Bill Gates comes to visit SoftImage and during an employee Q/A someone asks 'Since the Internet is all about free software, how come Microsoft is still charging for it?'" -- Adam Barr, __Proudly Serving My Corporate Masters__