Module: Globs
Overview
Container for Globs methods, currently only static as the public api footprint is relatively small.
Constant Summary collapse
- OPENING_BRACE =
Opening brace of a Glob expression
/\{/
- CLOSING_BRACE =
Closing brace of a Glob expression
/\}/
- SCANNER_INDEX_OFFSET =
StringScanner is not 0 indexed. Offset for index.
1
- BRACE_POSITION_OFFSET =
We don’t want to include the brace in our final set, so offset the index to compensate
1
- POSITION_OFFSET =
Full positional offset for the braces and scanner’s non-zero index
SCANNER_INDEX_OFFSET + BRACE_POSITION_OFFSET
- END_OF_STRING =
End of the string position, used to clarify difference between explicit EOS and positional offsets
-1
- VERSION =
"0.0.4"
Instance Method Summary collapse
-
#expand(string) ⇒ Array[String]
Expands a glob-like string into all possible interpretations of it.
-
#puts(string) ⇒ NilClass
Shorthand for ‘puts expand(str)` for outputting to STDOUT for unix-like piping.
Instance Method Details
#expand(string) ⇒ Array[String]
Modified to use StringScanner in 0.0.3 for more accurate tokenization
Expands a glob-like string into all possible interpretations of it.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/globs.rb', line 66 def (string) scanner = StringScanner.new(string) results = [''] until scanner.eos? beginning = scanner.pos start, finish = next_expression_positions!(scanner) # There are no further expressions in the string if the start position is # negative. # # Proceed to move the scanner's cursor to the end of the string and take # the rest of the string to append to the current result items. if start.negative? scanner.pos = string.size non_glob_str = string[beginning..END_OF_STRING] expressions = [''] else non_glob_str = string[beginning..(start - POSITION_OFFSET)] expressions = interpret_expression(string[start..finish]) end results = results.flat_map { |res| expressions.map { |exp| "#{res}#{non_glob_str}#{exp}" } } end results end |
#puts(string) ⇒ NilClass
Shorthand for ‘puts expand(str)` for outputting to STDOUT for unix-like piping.
43 44 45 |
# File 'lib/globs.rb', line 43 def puts(string) puts (string) end |