Module: Globs

Extended by:
Globs
Included in:
Globs
Defined in:
lib/globs.rb,
lib/globs/version.rb

Overview

Container for Globs methods, currently only static as the public api footprint is relatively small.

Author:

  • baweaver

Since:

  • 0.0.1

Constant Summary collapse

OPENING_BRACE =

Opening brace of a Glob expression

Since:

  • 0.0.1

/\{/
CLOSING_BRACE =

Closing brace of a Glob expression

Since:

  • 0.0.1

/\}/
SCANNER_INDEX_OFFSET =

StringScanner is not 0 indexed. Offset for index.

Since:

  • 0.0.1

1
BRACE_POSITION_OFFSET =

We don’t want to include the brace in our final set, so offset the index to compensate

Since:

  • 0.0.1

1
POSITION_OFFSET =

Full positional offset for the braces and scanner’s non-zero index

Since:

  • 0.0.1

SCANNER_INDEX_OFFSET + BRACE_POSITION_OFFSET
END_OF_STRING =

End of the string position, used to clarify difference between explicit EOS and positional offsets

Since:

  • 0.0.1

-1
VERSION =

Since:

  • 0.0.1

"0.0.4"

Instance Method Summary collapse

Instance Method Details

#expand(string) ⇒ Array[String]

Note:

Modified to use StringScanner in 0.0.3 for more accurate tokenization

Expands a glob-like string into all possible interpretations of it.

Examples:


```
Globs.expand("test.{a, b}.{1, 2}.com")
=> ["test.a.1.com", "test.a.2.com", "test.b.1.com", "test.b.2.com"]
```

Parameters:

  • string (String)

    Glob-like string to be expanded

Returns:

  • (Array[String])

    All expansions of the glob-like string

Since:

  • 0.0.1



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 expand(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.

Parameters:

  • string (String)

    Glob-like string to be expanded

Returns:

  • (NilClass)

    Only outputs to STDOUT, returning ‘nil` from the actual method call

Since:

  • 0.0.2



43
44
45
# File 'lib/globs.rb', line 43

def puts(string)
  puts expand(string)
end