Class: RegexForRange::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/regex_for_range/range.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#firstObject

Returns the value of attribute first.



3
4
5
# File 'lib/regex_for_range/range.rb', line 3

def first
  @first
end

#lastObject

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_regexObject



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