class Poodle::Blood
Constants
- BLOOD_GROUP
key: :phenotype value: [genotypes]
Public Class Methods
new(male, female, genotype: false)
click to toggle source
# File lib/poodle/blood.rb, line 14 def initialize(male, female, genotype: false) @male = male @female = female @genotype = genotype @blood_combination = [] end
Public Instance Methods
inherit()
click to toggle source
# File lib/poodle/blood.rb, line 21 def inherit valid_blood_type? if @genotype blood_combination.uniq else blood_combination.collect { |e| to_phenotype(e) }.uniq end end
inherit_with_rate()
click to toggle source
# File lib/poodle/blood.rb, line 30 def inherit_with_rate valid_blood_type? hash = blood_combination.reduce(Hash.new(0)) { |h,v| h[@genotype ? v : to_phenotype(v)] += 1 ; h } hash.each_with_object({}) { |(k,v), h| h[k] = v.to_f / blood_combination.size.to_f } end
Private Instance Methods
blood_combination()
click to toggle source
# File lib/poodle/blood.rb, line 38 def blood_combination @blood_combinaion ||= BLOOD_GROUP[@male].product(BLOOD_GROUP[@female]).collect { |set| set.sort.join } end
to_phenotype(string)
click to toggle source
# File lib/poodle/blood.rb, line 42 def to_phenotype(string) array = string.each_char.to_a.uniq array.reject! { |e| e == 'O' } unless array.size == 1 array.join # string end
valid_blood_type?()
click to toggle source
# File lib/poodle/blood.rb, line 48 def valid_blood_type? raise ArgumentError, "Invalid blood type" unless BLOOD_GROUP.key?(@male) && BLOOD_GROUP.key?(@female) true end