aboutsummaryrefslogtreecommitdiff
path: root/challenge-004
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-06 18:50:24 +0100
committerAbigail <abigail@abigail.be>2021-03-06 18:50:24 +0100
commit07d0ccdc3b3a291016a184219bb885bce49c5cd8 (patch)
tree2692d616c4261106101ed1ba41d78f705aa828fc /challenge-004
parent85799c6783f520b36021acf7e2cd93283056fe28 (diff)
downloadperlweeklychallenge-club-07d0ccdc3b3a291016a184219bb885bce49c5cd8.tar.gz
perlweeklychallenge-club-07d0ccdc3b3a291016a184219bb885bce49c5cd8.tar.bz2
perlweeklychallenge-club-07d0ccdc3b3a291016a184219bb885bce49c5cd8.zip
Ruby solution for week 4, part 2
Diffstat (limited to 'challenge-004')
-rw-r--r--challenge-004/abigail/README.md1
-rw-r--r--challenge-004/abigail/ruby/ch-2.rb42
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-004/abigail/README.md b/challenge-004/abigail/README.md
index 07a11c980b..75267e87b2 100644
--- a/challenge-004/abigail/README.md
+++ b/challenge-004/abigail/README.md
@@ -52,3 +52,4 @@ The sets of letters are read from standard input.
* [Node.js](node/ch-2.js)
* [Perl](perl/ch-2.pl)
* [Pyton](python/ch-2.py)
+* [Ruby](ruby/ch-2.rb)
diff --git a/challenge-004/abigail/ruby/ch-2.rb b/challenge-004/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..5dfcb0afce
--- /dev/null
+++ b/challenge-004/abigail/ruby/ch-2.rb
@@ -0,0 +1,42 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb < input-file
+#
+
+require "optparse"
+
+#
+# Parse options
+#
+params = ARGV . getopts ('f:')
+filename = params ["f"]
+
+
+#
+# Find words from the file 'filename' which can be made
+# from the letters in 'letters'.
+#
+def find_words (filename, letters)
+ letters = letters . downcase . split (//)
+ File . open (filename) do |f|
+ f . each_line do |word|
+ copy = word . downcase
+ letters . each do |letter|
+ copy . sub! letter, ""
+ end
+ if copy == ""
+ puts (word)
+ end
+ end
+ end
+end
+
+
+ARGF . each_line do |letters|
+ find_words filename, letters
+end