-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrybasic_caesar.rb
More file actions
executable file
·46 lines (37 loc) · 891 Bytes
/
crybasic_caesar.rb
File metadata and controls
executable file
·46 lines (37 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby
# frozen_string_literal: true
##
# Caesar cipher
class Caesar
DEFAULT_DICTIONARY = ('A'..'Z').to_a
def initialize(shift = 3, dictionary: DEFAULT_DICTIONARY)
@shift = shift
@dictionary = dictionary
end
def transform(text, dir = 1)
out = String.new
text.upcase.each_char do |char|
i = @dictionary.index(char)
if i.nil?
out << char
next
end
out << @dictionary[(i + @shift * dir) % @dictionary.length]
end
out
end
def encode(text)
transform(text, 1)
end
def decode(text)
transform(text, -1)
end
end
DICTIONARY = %w[А Б В Г Д Е Ё Ж З И Й К Л М Н О П Р С Т У Ф Х Ц Ч Ш Щ Ъ Ы Ь Э Ю Я].freeze
print 'Text: '
text = $stdin.readline
puts
DICTIONARY.length.times do |i|
caesar = Caesar.new(i, dictionary: DICTIONARY)
puts caesar.decode(text)
end