Class: Irv::Round

Inherits:
Object
  • Object
show all
Defined in:
lib/irv/round.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, candidates, votes) ⇒ Round

Returns a new instance of Round.



7
8
9
10
11
12
13
14
# File 'lib/irv/round.rb', line 7

def initialize(order, candidates, votes)
  @order = order
  @candidates = candidates
  @votes = votes
  @tallied_votes = tally(votes)
  @majority = calc_majority
  @loser = calc_loser
end

Instance Attribute Details

#loserObject (readonly)

Returns the value of attribute loser.



5
6
7
# File 'lib/irv/round.rb', line 5

def loser
  @loser
end

#majorityObject (readonly)

Returns the value of attribute majority.



5
6
7
# File 'lib/irv/round.rb', line 5

def majority
  @majority
end

#orderObject (readonly)

Returns the value of attribute order.



5
6
7
# File 'lib/irv/round.rb', line 5

def order
  @order
end

#tallied_votesObject (readonly)

Returns the value of attribute tallied_votes.



5
6
7
# File 'lib/irv/round.rb', line 5

def tallied_votes
  @tallied_votes
end

Instance Method Details

#exist_majority?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/irv/round.rb', line 16

def exist_majority?
  return true if @majority

  false
end

#next_candidatesObject



22
23
24
25
26
# File 'lib/irv/round.rb', line 22

def next_candidates
  return nil if exist_majority?

  @candidates.reject { |candidate| candidate == @loser }
end

#next_votesObject



28
29
30
31
32
33
34
# File 'lib/irv/round.rb', line 28

def next_votes
  return nil if exist_majority?

  @votes.map do |votes_in_a_rank|
    votes_in_a_rank.reject { |candidate| candidate == @loser }
  end
end