Class: Regex::Multiplicity

Inherits:
Object
  • Object
show all
Defined in:
lib/regex/multiplicity.rb

Overview

The multiplicity specifies by how much a given expression can be repeated.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aLowerBound, anUpperBound, aPolicy) ⇒ Multiplicity

Returns a new instance of Multiplicity.

Parameters:

  • aLowerBound (Integer)
  • anUpperBound (Integer, Symbol)

    integer or :more symbol

  • aPolicy (Symbol)

    One of: (:greedy, :lazy, :possessive)

Options Hash (aPolicy):

  • :greedy (Symbol)
  • :lazy (Symbol)
  • :possessive (Symbol)


22
23
24
25
26
# File 'lib/regex/multiplicity.rb', line 22

def initialize(aLowerBound, anUpperBound, aPolicy)
  @lower_bound = valid_lower_bound(aLowerBound)
  @upper_bound = valid_upper_bound(anUpperBound)
  @policy = valid_policy(aPolicy)
end

Instance Attribute Details

#lower_boundInteger (readonly)

Returns The lowest acceptable repetition count.

Returns:

  • (Integer)

    The lowest acceptable repetition count



7
8
9
# File 'lib/regex/multiplicity.rb', line 7

def lower_bound
  @lower_bound
end

#policySymbol

Returns An indicator that specifies how to repeat.

Returns:

  • (Symbol)

    An indicator that specifies how to repeat

See Also:



14
15
16
# File 'lib/regex/multiplicity.rb', line 14

def policy
  @policy
end

#upper_boundInteger, Symbol (readonly)

Returns The highest possible repetition count.

Returns:

  • (Integer, Symbol)

    The highest possible repetition count



10
11
12
# File 'lib/regex/multiplicity.rb', line 10

def upper_bound
  @upper_bound
end

Instance Method Details

#to_strString

Returns String representation of the multiplicity.

Returns:

  • (String)

    String representation of the multiplicity.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/regex/multiplicity.rb', line 29

def to_str
  case upper_bound
    when :more
      case lower_bound
        when 0
          subresult = '*'
        when 1
          subresult = '+'
        else
          subresult = "{#{lower_bound},}"
      end

    when lower_bound
      subresult = "{#{lower_bound}}"
    else
      if [lower_bound, upper_bound] == [0, 1]
        subresult = '?'
      else
        subresult = "{#{lower_bound},#{upper_bound}}"
      end
  end

  suffix = case policy
    when :greedy
      ''
    when :lazy
      '?'
    when :possessive
      '+'
  end

  return subresult + suffix
end