X-Git-Url: https://projects.mako.cc/source/rubyvote/blobdiff_plain/1217d8139176963d7c2693e76c0da7910631d9fa..HEAD:/lib/rubyvote/irv.rb diff --git a/lib/rubyvote/irv.rb b/lib/rubyvote/irv.rb index c3fabe6..aa94115 100644 --- a/lib/rubyvote/irv.rb +++ b/lib/rubyvote/irv.rb @@ -1,4 +1,18 @@ +# IRV is a preferential voting system used widely for government elections +# in Australia and New Zealand and elsewhere. IRV asks voters to rank +# candidates in preference and then holds a series of "runoff" elections +# by eliminating the weakest candidate and recomputing the election +# results until there exists a candidate who has a majority of the +# remaining votes. + +# Example:: + +# require 'irv' +# vote_array = [ ["A", "B"], ["B", "A"], ["B", "A"] ] +# resultobject = InstantRunoffVote.new(vote_array).result +# class InstantRunoffVote < ElectionVote + def initialize(votes=nil) @candidates = Array.new votes.each do |vote| @@ -15,22 +29,22 @@ class InstantRunoffVote < ElectionVote end protected - def tally_vote(vote) - votecopy = vote.dup - candidate = votecopy.shift - votes[candidate] = [0, Hash.new] unless votes.has_key?(candidate) - votes[candidate][0] += 1 - if votes[candidate][1].has_key?(votecopy) - votes[candidate][1][votecopy] += 1 - else - votes[candidate][1][votecopy] = 1 + def vote_valid?(vote = nil) + super && vote.is_a?(Array) && vote == vote.uniq + end + + def tally_vote(vote) + votecopy = vote.dup + candidate = votecopy.shift + votes[candidate] = [0, Hash.new] unless votes.has_key?(candidate) + votes[candidate][0] += 1 + if votes[candidate][1].has_key?(votecopy) + votes[candidate][1][votecopy] += 1 + else + votes[candidate][1][votecopy] = 1 + end end - end - def verify_vote(vote=nil) - vote.instance_of?( Array ) and - vote == vote.uniq - end end class InstantRunoffLogicVote < InstantRunoffVote