Class: Cipher
- Inherits:
-
Object
- Object
- Cipher
- Defined in:
- lib/cipher.rb
Instance Attribute Summary collapse
-
#plaintext ⇒ Object
readonly
Returns the value of attribute plaintext.
-
#shift_amount ⇒ Object
readonly
Returns the value of attribute shift_amount.
Instance Method Summary collapse
- #alpha_to_cipher ⇒ Object
- #alphabet ⇒ Object
- #ciphered_alphabet ⇒ Object
- #encrypt ⇒ Object
-
#initialize(shift_amount, plaintext) ⇒ Cipher
constructor
A new instance of Cipher.
Constructor Details
#initialize(shift_amount, plaintext) ⇒ Cipher
Returns a new instance of Cipher.
4 5 6 7 |
# File 'lib/cipher.rb', line 4 def initialize(shift_amount, plaintext) @shift_amount = shift_amount @plaintext = plaintext end |
Instance Attribute Details
#plaintext ⇒ Object (readonly)
Returns the value of attribute plaintext.
2 3 4 |
# File 'lib/cipher.rb', line 2 def plaintext @plaintext end |
#shift_amount ⇒ Object (readonly)
Returns the value of attribute shift_amount.
2 3 4 |
# File 'lib/cipher.rb', line 2 def shift_amount @shift_amount end |
Instance Method Details
#alpha_to_cipher ⇒ Object
28 29 30 |
# File 'lib/cipher.rb', line 28 def alpha_to_cipher Hash[alphabet.zip ciphered_alphabet] end |
#alphabet ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/cipher.rb', line 9 def alphabet ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] end |
#ciphered_alphabet ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/cipher.rb', line 19 def ciphered_alphabet append_to_end = alphabet[0..@shift_amount - 1] ciphered = alphabet.drop(@shift_amount) append_to_end.each do |letter| ciphered << letter end ciphered end |
#encrypt ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cipher.rb', line 32 def encrypt split_text = @plaintext.upcase.split("") encrypted = "" split_text.each do |letter| if letter == " " encrypted += letter elsif !alphabet.include?(letter) encrypted += "" else encrypted_letter = alpha_to_cipher[letter] encrypted += encrypted_letter end end encrypted.downcase end |