Class: Regexp::MatchLength
- Inherits:
-
Object
- Object
- Regexp::MatchLength
- Includes:
- Enumerable
- Defined in:
- lib/regexp_parser/expression/methods/match_length.rb
Class Method Summary collapse
Instance Method Summary collapse
- #each(opts = {}) ⇒ Object
- #endless_each ⇒ Object
- #fixed? ⇒ Boolean
- #include?(length) ⇒ Boolean
-
#initialize(exp, opts = {}) ⇒ MatchLength
constructor
A new instance of MatchLength.
- #inspect ⇒ Object
- #max ⇒ Object
- #min ⇒ Object
- #minmax ⇒ Object
- #to_re ⇒ Object
Constructor Details
#initialize(exp, opts = {}) ⇒ MatchLength
Returns a new instance of MatchLength.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 9 def initialize(exp, opts = {}) self.exp_class = exp.class self.min_rep = exp.repetitions.min self.max_rep = exp.repetitions.max if (base = opts[:base]) self.base_min = base self.base_max = base self.reify = ->{ '.' * base } else self.base_min = opts.fetch(:base_min) self.base_max = opts.fetch(:base_max) self.reify = opts.fetch(:reify) end end |
Class Method Details
Instance Method Details
#each(opts = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 24 def each(opts = {}) return enum_for(__method__, opts) unless block_given? limit = opts[:limit] || 1000 yielded = 0 (min..max).each do |num| next unless include?(num) yield(num) break if (yielded += 1) >= limit end end |
#endless_each ⇒ Object
35 36 37 38 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 35 def endless_each return enum_for(__method__) unless block_given? (min..max).each { |num| yield(num) if include?(num) } end |
#fixed? ⇒ Boolean
44 45 46 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 44 def fixed? min == max end |
#include?(length) ⇒ Boolean
40 41 42 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 40 def include?(length) test_regexp.match?('X' * length) end |
#inspect ⇒ Object
60 61 62 63 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 60 def inspect type = exp_class.name.sub('Regexp::Expression::', '') "#<#{self.class}<#{type}> min=#{min} max=#{max}>" end |
#max ⇒ Object
52 53 54 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 52 def max max_rep * base_max end |
#min ⇒ Object
48 49 50 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 48 def min min_rep * base_min end |
#minmax ⇒ Object
56 57 58 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 56 def minmax [min, max] end |
#to_re ⇒ Object
65 66 67 |
# File 'lib/regexp_parser/expression/methods/match_length.rb', line 65 def to_re "(?:#{reify.call}){#{min_rep},#{max_rep unless max_rep == Float::INFINITY}}" end |