aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/abigail/ruby/ch-1.rb
blob: bfa22541d8cf057a621293d7eea5421d0299619b (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
#!/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