-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar.rb
More file actions
34 lines (29 loc) · 767 Bytes
/
caesar.rb
File metadata and controls
34 lines (29 loc) · 767 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
def caesar_cipher (string, leap)
return "Invalid arguments" unless string.is_a?(String) && leap.is_a?(Integer)
leap %= 26
string.split("").map {|char| encode(char, leap)}.join
end
def encode (char, leap)
bound = nil
if char.between?('a','z')
bound = 'z'
elsif char.between?('A', 'Z')
bound ='Z'
end
if bound
shift_chars(char, leap, bound)
else
char
end
end
def shift_chars(char, shift, upper)
char_encoded_int = char.ord + shift
char_encoded_int -= 26 if char_encoded_int > upper.ord
char_encoded_int.chr
end
if $PROGRAM_NAME == __FILE__
puts caesar_cipher("What a string!", 5)
puts caesar_cipher("abcdefghijklmnopqrstuvwxyz", 263)
puts caesar_cipher("WhaT, As...", -2)
puts caesar_cipher("Some Message", "Bad")
end