Module: Picoglob

Defined in:
lib/picoglob.rb,
lib/picoglob/version.rb,
lib/picoglob/compiler.rb

Overview

Picoglob compiles bash-style glob patterns into Ruby Regexps so you can match *arbitrary strings* — S3 keys, routes, log lines, branch names — not just files on disk.

It’s the missing Ruby counterpart to JS’s picomatch / minimatch. Ruby ships ‘File.fnmatch` and `Dir.glob`, but neither gives you a reusable `Regexp`, and `File.fnmatch` has limited brace/extglob support that’s awkward to use off the filesystem.

Examples:

One-shot match

Picoglob.match?("src/**/*.{rb,erb}", "src/app/models/user.rb") # => true

Compile once, match many (fast)

g = Picoglob.new("logs/*.log")
g.match?("logs/app.log")   # => true
g.match?("logs/2026/x.log") # => false (single * doesn't cross "/")

Filter a list

Picoglob.filter("**/*.rb", ["a.rb", "lib/b.rb", "c.txt"]) # => ["a.rb", "lib/b.rb"]

Extglob

Picoglob.match?("image.+(jpg|png)", "image.png") # => true

Defined Under Namespace

Classes: Compiler, Matcher, ParseError

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.filter(pattern, strings, **opts) ⇒ Array<String>

Convenience: keep only the strings that match pattern.

Returns:

  • (Array<String>)


55
56
57
# File 'lib/picoglob.rb', line 55

def self.filter(pattern, strings, **opts)
  Matcher.new(pattern, **opts).filter(strings)
end

.match?(pattern, string, **opts) ⇒ Boolean

Convenience: does string match pattern?

Returns:

  • (Boolean)


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

def self.match?(pattern, string, **opts)
  Matcher.new(pattern, **opts).match?(string)
end

.new(pattern, **opts) ⇒ Picoglob::Matcher

Build a reusable matcher.

Parameters:

  • pattern (String)

    the glob pattern

  • separator (String)

    path separator that ‘*`/`?` won’t cross (default “/”)

  • dot (Boolean)

    match leading dots with wildcards (default false, shell-like)

  • extglob (Boolean)

    enable extglob syntax (default true)

  • nocase (Boolean)

    case-insensitive (default false)

Returns:



37
38
39
# File 'lib/picoglob.rb', line 37

def self.new(pattern, **opts)
  Matcher.new(pattern, **opts)
end

.to_regexp(pattern, **opts) ⇒ Regexp

Convenience: compile pattern to a Regexp.

Returns:

  • (Regexp)


49
50
51
# File 'lib/picoglob.rb', line 49

def self.to_regexp(pattern, **opts)
  Matcher.new(pattern, **opts).to_regexp
end