Class: RegexForRange::Range
- Inherits:
-
Object
- Object
- RegexForRange::Range
- Defined in:
- lib/regex_for_range/range.rb
Instance Attribute Summary collapse
-
#first ⇒ Object
Returns the value of attribute first.
-
#last ⇒ Object
Returns the value of attribute last.
Instance Method Summary collapse
-
#initialize(first, last) ⇒ Range
constructor
A new instance of Range.
- #overlaps(range) ⇒ Object
- #to_regex ⇒ Object
Constructor Details
#initialize(first, last) ⇒ Range
Returns a new instance of Range.
6 7 8 9 |
# File 'lib/regex_for_range/range.rb', line 6 def initialize(first, last) @first = first @last = last end |
Instance Attribute Details
#first ⇒ Object
Returns the value of attribute first.
3 4 5 |
# File 'lib/regex_for_range/range.rb', line 3 def first @first end |
#last ⇒ Object
Returns the value of attribute last.
4 5 6 |
# File 'lib/regex_for_range/range.rb', line 4 def last @last end |
Instance Method Details
#overlaps(range) ⇒ Object
11 12 13 |
# File 'lib/regex_for_range/range.rb', line 11 def overlaps(range) return self.last > range.first && range.last > self.first end |
#to_regex ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/regex_for_range/range.rb', line 15 def to_regex result = '' first_str = self.first.to_s last_str = self.last.to_s for i in (0..first_str.length - 1) if first_str[i] == last_str[i] result += first_str[i] else result += '[' + first_str[i] + '-' + last_str[i] + ']' end end return result end |