class Genome

Public Class Methods

new( arg ) click to toggle source
# File lib/kittyverse/genome.rb, line 5
def initialize( arg )
  if arg.is_a? Integer   ## use Integer (Fixnum+Bignum??) - why? why not?
    num = arg
    kai = Kai.encode( num )
  else
    if arg.downcase.start_with?( '0x' )  ## assume hexstring( base16 )
      kai = Kai.encode( arg.to_i(16) )
    else # else assume string in kai/base32 format
      kai = arg
      kai = kai.gsub( ' ', '' )   ## allow spaces (strip/remove)
    end
  end
  ## puts "Genome.initialize #{kai}"

  @kai   = kai    ## note: store/save kai without any spaces ("compact" format)
  @genes = build_genes( kai )  ## array of (sliced) genes (block of four genes)
end

Public Instance Methods

[](key) click to toggle source
# File lib/kittyverse/genome.rb, line 164
def [](key)
  if key.is_a? Integer   ## assume 0,1,2,3,.. index
    q , r = key.divmod(4)   ## q=quotient, r=rest/modulus
    ##  e.g.   3.divmod(4) => [0,3]
    ##         4.divmod(4) => [1,0]
    ##         5.divmod(4) => [1,1]  etc.
    @genes[q][r]
  else   ## assume trait type key / symbol
    @genes[index(key)]
  end
end
ac()
Alias for: colortertiary
bc()
Alias for: colorprimary
bin()
Alias for: binary
binary() click to toggle source
# File lib/kittyverse/genome.rb, line 33
def binary
  @kai.chars.each_slice(4).map do |slice|
    buf = ""
    buf << Kai::BINARY[slice[0]]
    buf << "-"
    buf << Kai::BINARY[slice[1]]
    buf << "-"
    buf << Kai::BINARY[slice[2]]
    buf << "-"
    buf << Kai::BINARY[slice[3]]
    buf
  end.join( " " )
end
Also aliased as: bin
body() click to toggle source
# File lib/kittyverse/genome.rb, line 74
def body()           @genes[0]; end
Also aliased as: fu
build_genes( kai ) click to toggle source
# File lib/kittyverse/genome.rb, line 49
  def build_genes( kai )
    kai = kai.reverse    ## note: reserve for easy left-to-right access
    genes = []   ## array of (sliced) genes (block of four genes)
    ## fix/todo: use as_json for "official" api order
    ## note: use insert order from "official" api

##    genes << Gene::Slice.new( :body, kai[0],
##                                     kai[1],
##                                     kai[2],
##                                     kai[3] )
##    genes << Gene::Slice.new( :pattern, kai[4+0],
##                                        kai[4+1],
##                                        kai[4+2],
##                                        kai[4+3]] )

    keys.each_with_index do |key,i|
       genes << Gene::Slice.new( key, kai[4*i+0],
                                      kai[4*i+1],
                                      kai[4*i+2],
                                      kai[4*i+3])
    end
    genes
  end
bytes() click to toggle source
# File lib/kittyverse/genome.rb, line 25
def bytes()  Kai.bytes( @kai ); end
codes()
Alias for: electrologica
color1()
Alias for: colorprimary
color2()
Alias for: colorsecondary
color3()
Alias for: colortertiary
coloreyes() click to toggle source
# File lib/kittyverse/genome.rb, line 76
def coloreyes()      @genes[2]; end
Also aliased as: ec
colorprimary() click to toggle source
# File lib/kittyverse/genome.rb, line 78
def colorprimary()   @genes[4]; end
Also aliased as: color1, bc
colorsecondary() click to toggle source
# File lib/kittyverse/genome.rb, line 79
def colorsecondary() @genes[5]; end
Also aliased as: color2, hc
colortertiary() click to toggle source
# File lib/kittyverse/genome.rb, line 80
def colortertiary()  @genes[6]; end
Also aliased as: color3, ac
each() { |slice| ... } click to toggle source
# File lib/kittyverse/genome.rb, line 107
def each()            @genes.each { |slice| yield(slice) }; end
Also aliased as: each_slice
each_gene() { |p| ... } click to toggle source
# File lib/kittyverse/genome.rb, line 112
def each_gene
  @genes.each do |slice|
    yield(slice.p)
    yield(slice.r1)
    yield(slice.r2)
    yield(slice.r3)
  end
end
each_gene_with_index() { |p, 4*i+0| ... } click to toggle source
# File lib/kittyverse/genome.rb, line 120
def each_gene_with_index
  @genes.each_with_index do |slice,i|
    yield(slice.p,  4*i+0)
    yield(slice.r1, 4*i+1)
    yield(slice.r2, 4*i+2)
    yield(slice.r3, 4*i+3)
  end
end
each_slice()
Alias for: each
each_slice_with_index()
Alias for: each_with_index
each_with_index() { |slice,i| ... } click to toggle source
# File lib/kittyverse/genome.rb, line 108
def each_with_index() @genes.each_with_index { |slice,i| yield(slice,i) }; end
Also aliased as: each_slice_with_index
ec()
Alias for: coloreyes
electrologica() click to toggle source
# File lib/kittyverse/genome.rb, line 30
def electrologica() Electrologica.fmt( Electrologica.encode( num ) ); end
Also aliased as: codes
en()
Alias for: environment
environment() click to toggle source
# File lib/kittyverse/genome.rb, line 83
def environment()    @genes[9]; end
Also aliased as: en
es()
Alias for: eyes
eyes() click to toggle source
# File lib/kittyverse/genome.rb, line 77
def eyes()           @genes[3]; end
Also aliased as: es
fu()

add (convenience) alias for two-letter (trait type) codes too

Alias for: body
hc()
Alias for: colorsecondary
index( key ) click to toggle source
# File lib/kittyverse/genome.rb, line 144
def index( key )
  if key.size == 2 && key =~ /^[A-Za-z]{2}$/ ## check for codes e.g. FU, PA, ... (or fu, pa,...)
    key = key.upcase.to_sym
    @@codes_by_index ||= %w(FU PA EC ES BC HC AC WE MO EN SE PU)
                         .each_with_index.reduce({}) do |h,(code,i)|
                                                       h[code.to_sym]=i; h
                                                     end
    @@codes_by_index[ key ]
  else
    key = key.downcase.to_sym
    key = ALT_TRAIT_TYPE_KEYS[ key ]  if ALT_TRAIT_TYPE_KEYS[ key ]

    @@keys_by_index ||= keys.each_with_index.reduce({}) do |h,(key,i)|
                                                          h[key]=i; h
                                                        end
    @@keys_by_index[ key ]
  end
end
kai() click to toggle source
# File lib/kittyverse/genome.rb, line 24
def kai()    Kai.fmt( @kai ); end
keys() click to toggle source
# File lib/kittyverse/genome.rb, line 129
def keys  ##  rename to trait_type_keys - why? why not?
  [:body,      ### todo/fix: use TRAITS.keys or something - why? why not?
   :pattern,
   :coloreyes,
   :eyes,
   :colorprimary,
   :colorsecondary,
   :colortertiary,
   :wild,
   :mouth,
   :environment,
   :secret,
   :prestige]
end
mo()
Alias for: mouth
mouth() click to toggle source
# File lib/kittyverse/genome.rb, line 82
def mouth()          @genes[8]; end
Also aliased as: mo
num() click to toggle source
# File lib/kittyverse/genome.rb, line 27
def num()    Kai.decode( @kai ); end
Also aliased as: to_i
pa()
Alias for: pattern
pattern() click to toggle source
# File lib/kittyverse/genome.rb, line 75
def pattern()        @genes[1]; end
Also aliased as: pa
prestige() click to toggle source
# File lib/kittyverse/genome.rb, line 85
def prestige()       @genes[11]; end
Also aliased as: pu
pu()
Alias for: prestige
se()
Alias for: secret
secret() click to toggle source
# File lib/kittyverse/genome.rb, line 84
def secret()         @genes[10]; end
Also aliased as: se
to_i()
Alias for: num
we()
Alias for: wild
wild() click to toggle source
# File lib/kittyverse/genome.rb, line 81
def wild()           @genes[7]; end
Also aliased as: we