-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar.rb
More file actions
35 lines (32 loc) · 862 Bytes
/
Caesar.rb
File metadata and controls
35 lines (32 loc) · 862 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
$upper_a_const = 65
$lower_a_const = 97
$upper_z_const = 90
$lower_z_const = 122
module Caesar
def Caesar.encrypt(original_string, shift)
new_string=""
shift_o = shift%25
original_string.each_byte { |i|
shift=shift_o
if (i >= $upper_a_const and i <= $upper_z_const)
if (i + shift) > $upper_z_const
distance = $upper_z_const - i + 1
shift = shift - distance
i = ($upper_a_const+shift).chr
else
i = (i + shift).chr
end
elsif (i >= $lower_a_const and i <= $lower_z_const)
if (i +shift) > $lower_z_const
distance = $lower_z_const - i + 1
shift = shift - distance
i = ($lower_a_const+shift).chr
else
i = (i + shift).chr
end
end
new_string = new_string+i
}
return new_string
end
end