Skip to content

Commit

Permalink
works in 1.8.7 && 1.9.3
Browse files Browse the repository at this point in the history
  • Loading branch information
David Heitzman committed Dec 9, 2012
1 parent 7347866 commit bdb54fe
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions ruby/gahelloworld.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module GAHelloWorld
RAND_SEED=srand
TARGET_GENE='Hello World!'
ALLOWED_LETTERS = (32.chr..122.chr).to_a
ALLOWED_LETTERS = (32..122).to_a.map{|i| i.chr}

class Chromosome
attr_reader :gene_ary, :target_ary, :gene
Expand All @@ -11,10 +11,11 @@ class << self
def gen_random()
str=''
TARGET_GENE.size.times do |i|
str << ALLOWED_LETTERS[rand(ALLOWED_LETTERS.size)]
str << ALLOWED_LETTERS[rand(ALLOWED_LETTERS.size)-1]
end
Chromosome.new(str)
end

def to_int_array(str)
#convenience method to get an array of strings for any string, compatible with ruby 1.9.3 and 1.8.7
out=[]
Expand Down Expand Up @@ -46,14 +47,6 @@ def mate partner
pivot = rand( gene_ary.size() - 1 )
ng1= gene[0..pivot] + partner.gene[pivot+1..-1]
ng2= partner.gene[0..pivot] + gene[pivot+1..-1]
# if RUBY_VERSION.include?("1.8")
# ng1 = ng1.map do |i| i.chr end.join
# ng2 = ng2.map do |i| i.chr end.join
# else
# ng1 = ng1.map do |i| i.chr end.join
# ng2 = ng2.map do |i| i.chr end.join
# end
puts "new chrome: " + ng1.to_s
[ Chromosome.new(ng1) , Chromosome.new(ng2) ]
end

Expand Down

0 comments on commit bdb54fe

Please sign in to comment.