diff options
| author | Abigail <abigail@abigail.be> | 2021-02-11 18:16:54 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-02-11 18:16:54 +0100 |
| commit | fe2f25f40d32e95166a8022ed5711e7e72b7350c (patch) | |
| tree | 81eeaea4b61c7b80e4ba7ca667bd07deb3baf764 /challenge-099/abigail/ruby | |
| parent | 3fa82a4182f389255a0ab940edf7ede4fcd5911d (diff) | |
| download | perlweeklychallenge-club-fe2f25f40d32e95166a8022ed5711e7e72b7350c.tar.gz perlweeklychallenge-club-fe2f25f40d32e95166a8022ed5711e7e72b7350c.tar.bz2 perlweeklychallenge-club-fe2f25f40d32e95166a8022ed5711e7e72b7350c.zip | |
Ruby solution for week 99, part 1
Diffstat (limited to 'challenge-099/abigail/ruby')
| -rw-r--r-- | challenge-099/abigail/ruby/ch-1.rb | 29 |
1 files changed, 29 insertions, 0 deletions
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 |
