diff options
| -rw-r--r-- | challenge-099/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-099/abigail/ruby/ch-1.rb | 29 |
2 files changed, 30 insertions, 0 deletions
diff --git a/challenge-099/abigail/README.md b/challenge-099/abigail/README.md index eed4a2dca1..6a91e3100c 100644 --- a/challenge-099/abigail/README.md +++ b/challenge-099/abigail/README.md @@ -48,6 +48,7 @@ Output: 1 * [Using regular expressions](perl/ch-1.pl) * [Recursion](perl/ch-1a.pl) * [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ## [Unique Subsequence](https://perlweeklychallenge.org/blog/perl-weekly-challenge-099/#TASK2) diff --git a/challenge-099/abigail/ruby/ch-1.rb b/challenge-099/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..bfa22541d8 --- /dev/null +++ b/challenge-099/abigail/ruby/ch-1.rb @@ -0,0 +1,29 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +# +# Read a string and a pattern. Turn the pattern into a regular expression: +# - '?' becames '.' +# - '*' becomes '.*' +# - any other character will be quotemetaed. +# +# Then map the string against the pattern, anchored. +# + +ARGF . each_line do |_| + (string, pattern) = _ . strip . split + pattern . gsub! (/./) {|_| + _ == "?" ? "." + : _ == "*" ? ".*" + : (_ . match (/\W/)) ? "\\" + _ + : _} + pattern = "^" + pattern + "$" + puts (string . match (pattern)) ? 1 : 0 +end |
