Class: Cryptogram

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptograms/cryptogram.rb

Constant Summary collapse

ALPHABET =
(?A..?Z).to_a.map(&:freeze).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plaintext) ⇒ Cryptogram

Returns a new instance of Cryptogram.



5
6
7
8
# File 'lib/cryptograms/cryptogram.rb', line 5

def initialize plaintext
  @plaintext = plaintext.upcase
  @cyphertext = @plaintext.tr ALPHABET.join, cypher.join
end

Instance Attribute Details

#cyphertextObject (readonly)

Returns the value of attribute cyphertext.



2
3
4
# File 'lib/cryptograms/cryptogram.rb', line 2

def cyphertext
  @cyphertext
end

#keyObject (readonly)

Returns the value of attribute key.



2
3
4
# File 'lib/cryptograms/cryptogram.rb', line 2

def key
  @key
end

#plaintextObject (readonly)

Returns the value of attribute plaintext.



2
3
4
# File 'lib/cryptograms/cryptogram.rb', line 2

def plaintext
  @plaintext
end

Instance Method Details

#cypherObject

Randomize until no cyphertext replacement is the same as the original letter.



12
13
14
15
16
17
18
# File 'lib/cryptograms/cryptogram.rb', line 12

def cypher
  loop do
    (@key ||= ALPHABET.dup).shuffle!
    redo if @key.zip(ALPHABET).any? { |k, v| k == v }
    return @key
  end
end