Class: Wildcard

Inherits:
Object
  • Object
show all
Defined in:
lib/joker.rb,
ext/joker_native/Joker.c

Overview

Implements wildcards for Ruby. Modeled after the Regexp class.

This implementation supports the following special characters:

  • ? matches a single character

    • matches any number of characters, including 0

  • * matches a literal ‘*’

  • \? matches a literal ‘?’

  • \\ matches a literal ‘\’

  • xyz

    matches either ‘x’, ‘y’ or ‘z’. NOTE that you have to escape ‘]’ in these groups: \]

NOTE that ‘\a’ will match the literal string ‘\a’, not ‘a’ as one might expect.

wild = Wildcard['Fairy?ake*']
wild =~ 'Fairycake'                     #=> true
wild =~ 'Fairyfakes are mean'           #=> true
wild =~ 'Fairysteakes are delicious'    #=> false

Also there is a case sensitivity flag. By default it is set to true, but it can be turned off at construction time:

wild = Wildcard['Fairy?ake*', true]
wild =~ 'Fairycake'
wild =~ 'fairyCAKE'
wild =~ 'FaIrYfAkEs, the Movie'

Furthermore, any given Wildcard expression must match the whole string:

wild = Wildcard['Fairy?ake']
wild =~ 'some Fairycake'                #=> false
wild =~ 'Fairycake is good for you'     #=> false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wildcard_string, casefold = false) ⇒ Wildcard

Creates a new Wildcard from the given string. If casefold is true, the Wildcard will ignore case. TODO



61
62
63
64
65
66
67
68
# File 'lib/joker.rb', line 61

def initialize( wildcard_string, casefold = false )
    @source   = wildcard_string
    @casefold = casefold
    @regexp   =
        if casefold then Regexp.new(compile, Regexp::IGNORECASE)
        else             Regexp.new(compile)
        end
end

Instance Attribute Details

#casefoldObject (readonly) Also known as: casefold?

Boolean. Determines case sensitivity of the Wildcard.

If this is true, the Wildcard will ignore case.



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

def casefold
  @casefold
end

#sourceObject (readonly)

The string from which the Wildcard was constructed.



54
55
56
# File 'lib/joker.rb', line 54

def source
  @source
end

Class Method Details

.newObject Also known as: [], compile

.quote(string) ⇒ Object Also known as: escape

Returns a new string with any characters escaped that would have special meaning in a Wildcard.



76
77
78
# File 'lib/joker.rb', line 76

def quote( string )
    string.gsub(%r{[\\?*\[\]]}) { |char| "\\#{char}" }
end

Instance Method Details

#===Object

#=~Object

#eql?(that) ⇒ Boolean Also known as: ==

Compares to Wildcards for equality.

Two wildcards are equal, if they were constructed from the same string and have the same #casefold?().

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/joker.rb', line 106

def eql?( that )
    return false unless that.is_a?(Wildcard)
    self.source == that.source && self.casefold == that.casefold
end

#inspectObject



86
87
88
# File 'lib/joker.rb', line 86

def inspect
    %{Wildcard[#{self.source.inspect}]#{self.casefold ? 'i' : ''}}
end

#~Object

Matches the wildcard against $_:

$_ = 'I love fairycakes'
~Wildcard['*fairy*']        #=> true


96
97
98
# File 'lib/joker.rb', line 96

def ~
    self =~ $_
end