Sha256: 810fb1d627c828a9ff9225effc2bb12aab8141d1034622370efa339e9fca0f05

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module RubyQuiz1
  class CharacterGroup

    def initialize(value, solitaire=nil)
      raise ArgumentError unless String(value) =~ /\A[A-Z]{1,5}\z/
      @value = String(value)
      @keystream = generate_keystream(solitaire) if solitaire
    end

    def characters
      pad(value)
    end

    def numbers
      @numbers ||= characters.chars.collect do |c|
        ("A".."Z").to_a.index(c) + 1
      end
    end

    def encrypted
      return unless keystream
      convert_to_characters(encrypted_numbers)
    end

    def decrypted
      return unless keystream
      convert_to_characters(decrypted_numbers)
    end

    def ==(other)
      characters == other.characters
    end

  private

    def value
      @value
    end

    def pad(value)
      value + "X" * (5 - value.length)
    end

    def keystream
      @keystream
    end

    def generate_keystream(solitaire)
      self.class.new(characters.chars.collect { |l| solitaire.next_key }.join)
    end

    def encrypted_numbers
      numbers.collect.with_index do |n, i|
        e = n + keystream.numbers[i]
        e -= 26 if e > 26
        e
      end
    end

    def decrypted_numbers
      numbers.collect.with_index do |n, i|
        k = keystream.numbers[i]
        n += 26 if n <= k
        n - k
      end
    end

    def convert_to_characters(numbers)
      numbers.collect { |n| ("A".."Z").to_a[n - 1] }.join
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby_quiz_1-0.0.2 lib/ruby_quiz_1/character_group.rb
ruby_quiz_1-0.0.1 lib/ruby_quiz_1/character_group.rb