Module: Dartboard

Defined in:
lib/dartboard.rb

Defined Under Namespace

Modules: Version

Class Method Summary collapse

Class Method Details

.throw(hash_of_object_to_numbers, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dartboard.rb', line 10

def self.throw(hash_of_object_to_numbers, options={})
  if options[:percentage]
    dup_hash = hash_of_object_to_numbers.dup

    dup_hash.each do |key, value|
      dup_hash[key] = value * 100
    end

    throw(dup_hash, :percentage => false)
  else
    array = []

    hash_of_object_to_numbers.each do |obj, percentage|
      1.upto(percentage.round) do
        array.push(obj)
      end
    end

    array.sort_by { rand }[0]
  end
end