diff options
| author | Abigail <abigail@abigail.be> | 2021-01-26 18:24:43 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-01-26 18:25:50 +0100 |
| commit | ae613406d9d15aaecda3bf823c09cc50af180b5b (patch) | |
| tree | 60e61eeca95fd1a887cb719603af65622f632b6e /challenge-097 | |
| parent | 6b9d80211e0e0330704638b9d2ac74c6fc2b4eba (diff) | |
| download | perlweeklychallenge-club-ae613406d9d15aaecda3bf823c09cc50af180b5b.tar.gz perlweeklychallenge-club-ae613406d9d15aaecda3bf823c09cc50af180b5b.tar.bz2 perlweeklychallenge-club-ae613406d9d15aaecda3bf823c09cc50af180b5b.zip | |
Ruby solution for week 97, part 1
Diffstat (limited to 'challenge-097')
| -rw-r--r-- | challenge-097/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-097/abigail/ruby/ch-1.rb | 46 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-097/abigail/README.md b/challenge-097/abigail/README.md index ef9eaa5301..0e2295286a 100644 --- a/challenge-097/abigail/README.md +++ b/challenge-097/abigail/README.md @@ -31,6 +31,7 @@ to indicate the left shift. * [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ### Blog diff --git a/challenge-097/abigail/ruby/ch-1.rb b/challenge-097/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..1225bd290c --- /dev/null +++ b/challenge-097/abigail/ruby/ch-1.rb @@ -0,0 +1,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 |
