Skip to content
/ irv Public

A Ruby library providing Instant-Runoff Voting

License

Notifications You must be signed in to change notification settings

highwide/irv

Repository files navigation

Irv

Gem Version Build Status

This gem provides Instant-Runoff Voting for Ruby program.

Installation

Add this line to your application's Gemfile:

gem 'irv'

And then execute:

$ bundle

Or install it yourself as:

$ gem install irv

Usage

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'

Why George was choosen in this example?

Round 1

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.

Round 2

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

Round 3

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.

If you want to know these stuff in a Ruby program

# 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:

Development

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.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Irv project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

A Ruby library providing Instant-Runoff Voting

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published