Class String::Mask
In: lib/more/facets/string/mask.rb
Parent: Object

Methods

&   *   +   -   ==   []   []   ^   apply   inspect   instance_delegate   mask   mask!   method_missing   new   replace   to_s   |  

Constants

ESC = "\032"

Attributes

string  [R] 

Public Class methods

[Source]

# File lib/more/facets/string/mask.rb, line 18
    def [](string, re=nil)
      new(string, re)
    end

[Source]

# File lib/more/facets/string/mask.rb, line 30
    def initialize(string, re=nil)
      @string = string.dup
      mask!(re) if re
    end

Public Instance methods

Mask AND. Only where they are then same filters through.

    "abc..123"      "ab..789."
  & "ab..789."    | "abc..123"
    ----------      ----------
    "ab......"      "ab......"

[Source]

# File lib/more/facets/string/mask.rb, line 132
    def &(other)
      i = 0
      o = ''
      while i < string.size
        if (c = string[i,1]) == other[i,1]
          o << c
        else
          o << ESC
        end
        i += 1
      end
      self.class.new(o)
    end

Mask XAND. Where the characters are the same, the result is the same, where they differ the result reflects the later.

    "abc..123"      "ab..789."
  * "ab..789."    | "abc..123"
    ----------      ----------
    "ab..789."      "abc..123"

[Source]

# File lib/more/facets/string/mask.rb, line 110
    def *(other)
      i = 0
      o = ''
      while i < string.size
        if (c = string[i,1]) == other[i,1]
          o << c
        else
          o << other[i,1]
        end
        i += 1
      end
      self.class.new(o)
    end

Mask ADD. As long as there is a value other then empty the character filters though. The last string takes precedence.

    "abc..123"      "ab..789."
  + "ab..789."    + "abc..123"
    ----------      ----------
    "abc.7893"      "abc.7123"

[Source]

# File lib/more/facets/string/mask.rb, line 84
    def +(other)
      i = 0
      o = ''
      while i < string.size
        if other[i,1] == ESC
          o << string[i,1]
        else
          o << other[i,1]
        end
        i += 1
      end
      self.class.new(o)
    end

Mask subtraction. Where the characters are the same, the result is "empty", where they differ the result reflects the last string.

    "abc..123"      "ab..789."
  - "ab..789."    - "abc..123"
    ----------      ----------
    "....789."      "..c..123"

[Source]

# File lib/more/facets/string/mask.rb, line 61
    def -(other)
      i = 0
      o = ''
      while i < string.size
        if string[i,1] == other[i,1]
          o << ESC
        else
          o << other[i,1]
        end
        i += 1
      end
      self.class.new(o)
    end

[Source]

# File lib/more/facets/string/mask.rb, line 171
    def ==(other)
      case other
      when String::Mask
        string == other.string
      else
        string == other.to_s
      end
    end

[Source]

# File lib/more/facets/string/mask.rb, line 40
    def [](*a)
      string[*a]
    end

Mask XOR operation. Only where there is an empty slot will the value filter.

    "abc..123"      "ab..789."
  | "ab..789."    | "abc..123"
    ----------      ----------
    "..c.7..3"      "..c.7..3"

[Source]

# File lib/more/facets/string/mask.rb, line 154
    def ^(other)
      i = 0
      o = ''
      while i < string.size
        if string[i,1] == ESC
          o << other[i,1]
        elsif other[i,1] == ESC
          o << string[i,1]
        else
          o << ESC
        end
        i += 1
      end
      self.class.new(o)
    end

Apply a method to the internal string and return a new mask.

[Source]

# File lib/more/facets/string/mask.rb, line 182
    def apply(s=nil,*a,&b)
      if s
        string.send(s,*a,&b).to_mask
      else
        @_self ||= Functor.new do |op, *a|
          string.send(op,*a).to_mask
        end
      end
    end

[Source]

# File lib/more/facets/string/mask.rb, line 37
    def inspect ; string.inspect ; end

[Source]

# File lib/more/facets/string/mask.rb, line 196
    def instance_delegate
      @string
    end

[Source]

# File lib/more/facets/string/mask.rb, line 44
    def mask(re)
      self.class.new(string,re)
    end

[Source]

# File lib/more/facets/string/mask.rb, line 48
    def mask!(re)
      string.gsub!(re){ |s| ESC * s.size }
    end

Delegate any missing methods to underlying string.

[Source]

# File lib/more/facets/string/mask.rb, line 209
    def method_missing(s, *a, &b)
      begin
        str = string.send(s, *a, &b)
      rescue NoMethodError
        super(s, *a, &b)
      end
    end

[Source]

# File lib/more/facets/string/mask.rb, line 192
    def replace(string)
      @string = string.to_s
    end

[Source]

# File lib/more/facets/string/mask.rb, line 38
    def to_s    ; string ; end
|(other)

Alias for #+

[Validate]