Module: RIEL::StringExt
- Defined in:
- lib/riel/string.rb
Constant Summary collapse
- Infinity =
Represents infinity, for the
to_rangesfunction. (1.0 / 0)
- RANGE_REGEXP =
:stopdoc:
from (maybe to this) %r{^ \s* (\d*) (?: \s* (\-|\.\.) \s* (\d*) )? \s* $ }x
Instance Method Summary collapse
-
#-(other) ⇒ Object
Returns a string based on this instance, with the first occurrance of
otherremoved. -
#_has_matchdata?(md, idx) ⇒ Boolean
:stopdoc:.
- #_matchdata_to_number(md, idx, default) ⇒ Object
-
#ends_with(substr) ⇒ Object
Returns whether the string ends with the given substring.
-
#num ⇒ Object
Returns the string, as a number if it is one, and nil otherwise,.
-
#to_ranges(args = Hash.new) ⇒ Object
Splits num into array of ranges, limiting itself to
args[:min]andargs[:max], if specified.
Instance Method Details
#-(other) ⇒ Object
Returns a string based on this instance, with the first occurrance of other removed. other may be a string or regular expression.
37 38 39 |
# File 'lib/riel/string.rb', line 37 def - other sub other, '' end |
#_has_matchdata?(md, idx) ⇒ Boolean
:stopdoc:
93 94 95 |
# File 'lib/riel/string.rb', line 93 def _has_matchdata? md, idx md && md[idx] && !md[idx].empty? end |
#_matchdata_to_number(md, idx, default) ⇒ Object
97 98 99 |
# File 'lib/riel/string.rb', line 97 def _matchdata_to_number md, idx, default _has_matchdata?(md, idx) ? md[idx].to_i : default end |
#ends_with(substr) ⇒ Object
Returns whether the string ends with the given substring.
18 19 20 |
# File 'lib/riel/string.rb', line 18 def ends_with substr return rindex(substr) == length - substr.length end |
#num ⇒ Object
Returns the string, as a number if it is one, and nil otherwise,
25 26 27 28 29 30 31 |
# File 'lib/riel/string.rb', line 25 def num begin Integer self rescue ArgumentError nil end end |
#to_ranges(args = Hash.new) ⇒ Object
Splits num into array of ranges, limiting itself to args[:min] and args[:max], if specified. The args[:collapse], if true, results in sequential values put into the same range.
Examples:
"1,2".to_ranges # [ 1 .. 1, 2 .. 2 ]
"1,2".to_ranges :collapse => true # [ 1 .. 2 ]
"1-4".to_ranges # [ 1 .. 4 ]
"1-3,5-6,10,11,12,".to_ranges :collapse => true # [ 1 .. 3, 5 .. 6, 10 .. 12 ]
"1-3,5-6,10,11,12,".to_ranges # [ 1 .. 3, 5 .. 6, 10 .. 10, 11 .. 11, 12 .. 12 ]
"-4".to_ranges # [ -String::Infinity .. 4 ]
"4-".to_ranges # [ 4 .. String::Infinity ]
"1-".to_ranges :min => 0, :max => 8 # [ 1 .. 8 ]
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/riel/string.rb', line 67 def to_ranges args = Hash.new min = args[:min] || -Infinity max = args[:max] || Infinity collapse = args[:collapse] ranges = Array.new self.split(%r{\s*,\s*}).each do |section| md = section.match(RANGE_REGEXP) next unless md from = _matchdata_to_number md, 1, min to = _has_matchdata?(md, 2) ? _matchdata_to_number(md, 3, max) : from prevrange = ranges[-1] if collapse && prevrange && prevrange.include?(from - 1) && prevrange.include?(to - 1) ranges[-1] = (prevrange.first .. to) else ranges << (from .. to) end end ranges end |