Class: Key::SQKey

Inherits:
SKey show all
Defined in:
lib/key/sqkey.rb

Overview

SQKey

Class for handling keys generated by a Polybius square.

XXX only 6x6 square is implemented in order to

  1. have the full alphabet

  2. have numbers as well

It also simplify the code

Constant Summary

BASE36 =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
SQ_NUMBERS =
1
SQ_ADFGVX =
2
CODE_WORD =
{
    SQ_NUMBERS => [ 0, 1, 2, 3, 4, 5 ],
    SQ_ADFGVX  => 'ADFGVX'.each_char.to_a
}

Constants inherited from SKey

Key::SKey::BASE

Instance Attribute Summary (collapse)

Attributes inherited from SKey

#alpha, #ralpha

Instance Method Summary (collapse)

Methods inherited from SKey

#decode, #encode

Constructor Details

- (SQKey) initialize(key, type = SQ_ADFGVX)

Returns a new instance of SQKey



37
38
39
40
41
42
43
44
45
# File 'lib/key/sqkey.rb', line 37

def initialize(key, type = SQ_ADFGVX)
  super(key.gsub(%r{\s*}, ''))
  @alpha = Hash.new
  @ralpha = Hash.new
  @type = type
  @base = BASE36
  @full_key = (@key + @base).condensed
  gen_rings
end

Instance Attribute Details

- (Object) full_key (readonly)

Returns the value of attribute full_key



35
36
37
# File 'lib/key/sqkey.rb', line 35

def full_key
  @full_key
end

- (Object) type (readonly)

Returns the value of attribute type



35
36
37
# File 'lib/key/sqkey.rb', line 35

def type
  @type
end

Instance Method Details

- (Object) gen_rings

gen_rings

Assign a code number/letter for each letter.

Generate both the encoding and decoding rings.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/key/sqkey.rb', line 53

def gen_rings
  ind = 0
  word = @full_key.dup
  CODE_WORD[@type].each do |i|
    CODE_WORD[@type].each do |j|
      c = word[ind]
      @alpha[c.chr] = "#{i}#{j}"
      @ralpha["#{i}#{j}"] = c.chr
      ind += 1
    end
  end
end