This gem provides Instant-Runoff Voting for Ruby program.
Add this line to your application's Gemfile:
gem 'irv'
And then execute:
$ bundle
Or install it yourself as:
$ gem install irv
require 'irv'
irv = Irv.new(['John', 'Paul', 'Ringo', 'George'])
irv
.poll!(['John', 'George', 'Ringo'])
.poll!(['John', 'Ringo', 'George', 'Paul'])
.poll!(['Paul', 'George', 'Ringo', 'John'])
.poll!(['Ringo', 'Paul', 'George'])
.poll!(['George', 'Ringo', 'John'])
puts irv.winner
# => 'George'
A | B | C | D | E | |
---|---|---|---|---|---|
John | 1 | 1 | 4 | - | 3 |
Paul | - | 4 | 1 | 2 | - |
Ringo | 3 | 2 | 3 | 1 | 2 |
George | 2 | 3 | 2 | 3 | 1 |
John got two first ranked votes. But it wasn't the majority. So, Election goes on without last place candidate.
In this case, Paul, Ringo and George got one first ranked vote. How was second ranked vote? Paul got only one vote. Ringo and George got two. Therefore Paul got lose in this round.
A | B | C | D | E | |
---|---|---|---|---|---|
John | 1 | 1 | 3 | - | 3 |
Ringo | 3 | 2 | 2 | 1 | 2 |
George | 2 | 3 | 1 | 2 | 1 |
John and George got two first ranked votes. Ringo got one. So, Ringo got lose in this round
A | B | C | D | E | |
---|---|---|---|---|---|
John | 1 | 1 | 2 | - | 2 |
George | 2 | 2 | 1 | 1 | 1 |
George got three first ranked votes! It was a majority. So George became winner.
# After polled...
result = irv.result
result.process.each { |pr| puts "round: #{pr.order} /majority: #{pr.majority} / loser: #{pr.loser}" }
# => round: 1 /majority: / loser: Paul
# => round: 2 /majority: / loser: Ringo
# => round: 3 /majority: George / loser:
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/highwide/irv. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Irv project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.