Class: Range

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

Overview

Range extensions to parse text.

Class Method Summary collapse

Class Method Details

.parse(text) ⇒ Object

Parse the text to a pair of tokens and return these as a new range object.

The separator may be two dots or three dots:

* Two dots ".." includes the stop item.
* Three dots "..." excludes the stop item.

This method delegates parsing of each tokens to Range.parse_helper

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sixarm_ruby_range_parse/range.rb', line 17

def self.parse(text)
  begin
    text=text.to_s
    if text=~/(\.\.\.?)/
      start_token = $`
      stop_token = $'
      separator = $1
      exclude_end = (separator == "...")
      return self.new(self.parse_helper(start_token), self.parse_helper(stop_token), exclude_end)
    end
  rescue
  end
  raise ArgumentError.new("#parse text must have a start token, two or three dots, and a stop token; this text does not parse: \"#{text}\"")
end

.parse_helper(text) ⇒ Object

Parse one item of the pair of items. Subclasses will likely want to override this.



35
36
37
# File 'lib/sixarm_ruby_range_parse/range.rb', line 35

def self.parse_helper(text)
  text
end