Sha256: a8f73ba4b892e6253439b24cfa18c1ea5ebea3904fa2b95c236146f508d81470
Contents?: true
Size: 779 Bytes
Versions: 133
Compression:
Stored size: 779 Bytes
Contents
# frozen_string_literal: true module EacRubyUtils class RegexpParser attr_reader :pattern, :builder_proc def initialize(pattern, &builder_proc) @pattern = pattern @builder_proc = builder_proc end def parse(string) internal_parse(string)[1] end def parse!(string) match, result = internal_parse(string) return result if match raise ::ArgumentError, "String \"#{string}\" does not match pattern \"#{pattern}\"" end # @return [Boolean] def parse?(string) internal_parse(string).first end private def internal_parse(string) m = pattern.match(string) if m [true, builder_proc ? builder_proc.call(m) : m] else [false, nil] end end end end
Version data entries
133 entries across 133 versions & 2 rubygems