런타임에 내 코드는 종종 mate
메서드에 대해 정의되지 않은 메서드 오류가 발생합니다. 내가 생각할 수있는 한, Person
은 어떻게 든 코드 excution을 따라 언제든지 균열을 통과하여 allele
이 할당되지 않도록 관리합니다.어떻게 든 Ruby 클래스를 할당하지 않음
코드 (가장 좋은 포맷되지 면책 조항) :
class Allele
attr_accessor :c1, :c2
def initialize(c1, c2)
@c1 = c1
@c2 = c2
end
#formats it to be readable
def to_s
c1.to_s + c2.to_s
end
#given Allele a
def combine(a)
pick = rand(4)
case pick
when 0
Allele.new(c1,a.c1)
when 1
Allele.new(c1,a.c2)
when 2
Allele.new(c2,a.c1)
when 3
Allele.new(c2,a.c2)
end
end
end
class Person
attr_accessor :allele, :male
def initialize(allele,male)
@allele = allele
@male= male
end
#def self.num_people
#@@num_people
#end
def to_s
"Allele:" + allele.to_s + " | Male:" + male.to_s
end
def male
@male
end
def allele
@allele
end
def mate(p)
if rand(2) == 0
Person.new(allele.combine(p.allele),true)
else
Person.new(allele.combine(p.allele),false)
end
end
end
male_array = Array.new
female_array = Array.new
male_child_array = Array.new
female_child_array = Array.new
# EVENLY POPULATE THE ARRAY WITH 5 THAT PHENOTYPICALLY MANIFEST TRAIT, 5 THAT DON'T
# AND 5 GIRLS, 5 GUYS
pheno_dist = rand(5)
#make guys with phenotype
pheno_dist.times { male_array << Person.new(Allele.new(1,rand(2)),true) }
#guys w/o
(5-pheno_dist).times { male_array << Person.new(Allele.new(0,0),true) }
#girls w/ pheno
(5-pheno_dist).times { female_array << Person.new(Allele.new(1,rand(2)),false) }
#girls w/o
pheno_dist.times { female_array << Person.new(Allele.new(0,0),false) }
puts male_array
puts female_array
puts "----------------------"
4.times do
#mates male with females, adding children to children arrays. deletes partners as it iterates
male_array.each do
male_id = rand(male_array.length) #random selection function. adjust as needed
female_id = rand(female_array.length)
rand(8).times do
child = male_array[male_id].mate(female_array[female_id])
if child.male
male_child_array << child
else
female_child_array << child
end
end
male_array.delete_at(male_id)
female_array.delete_at(female_id)
end
#makes males male children, females female children, resets child arrays
male_array = male_child_array
female_array = female_child_array
male_child_array = []
female_child_array = []
puts male_array
puts female_array
puts "----------------------"
end
무엇 즉시 잘못 보인다?
코드 크기입니다. 관련 부분을 게시하는 것이 훨씬 더 이해할 수 있습니다. – johannes
나는 어느 정도 생각했지만, 실제로 오류를 그려내는 곳을 직접 추적 할 수 없었기 때문에, 실제로 내가 단축 된 부분 (배열 삭제)을 편집했을 것입니다. 그러나 일했다. – deeb