blob: 1225bd290ccbed9f2025b81759816ee2044e8dd2 (
plain)
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/ruby
#
# See ../README.md
#
#
# Run as: ruby ch-1.rb -s SHIFT < input-file
#
require 'optparse'
NR_OF_LETTERS = 26
#
# Parse and validate options
#
params = ARGV . getopts ('s:')
shift = params ["s"] ? params ["s"] . to_i % NR_OF_LETTERS : -1
if shift < 0
STDERR . puts "Requires a -s SHIFT option"
exit 1
end
#
# Method to shift an upper case letter
#
def shift_letter (letter, shift)
n = letter . ord - shift
if n < 'A' . ord
then n = n + NR_OF_LETTERS
end
return n . chr
end
#
# Iterate over the input, shift capital letters
#
ARGF . each_line do |line|
line = line . gsub (/[A-Z]/) {|_| shift_letter _, shift}
puts line
end
|