class String::Mask

Mask

Constants

ESC

Substitue (TODO: rename)

VERSION

Current version.

Public Class Methods

[](string, re=nil) click to toggle source

New Mask.

@param [String] string

Any regular or masked string.

@param [String] re

Single character string used to mark empty slots.
# File lib/strmask.rb, line 31
def self.[](string, re=nil)
  new(string, re)
end
new(string, re=nil) click to toggle source

Initialize new string mask.

@param [String] string

Any regular or masked string.

@param [String] re

Single character string used to mark empty slots.
# File lib/strmask.rb, line 43
def initialize(string, re=nil)
  @to_str = string.dup
  @re     = re
  mask!(re) if re
end

Public Instance Methods

&(other) click to toggle source

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

  "abc..123"      "ab..789."
& "ab..789."    | "abc..123"
  ----------      ----------
  "ab......"      "ab......"
# File lib/strmask.rb, line 169
def &(other)
  other = convert(other)
  i = 0
  o = ''
  while i < to_str.size
    if (c = to_str[i,1]) == other[i,1]
      o << c
    else
      o << ESC
    end
    i += 1
  end
  self.class.new(o, @re)
end
*(other) click to toggle source

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"
# File lib/strmask.rb, line 146
def *(other)
  other = convert(other)
  i = 0
  o = ''
  while i < to_str.size
    if (c = to_str[i,1]) == other[i,1]
      o << c
    else
      o << other[i,1]
    end
    i += 1
  end
  self.class.new(o, @re)
end
+(other) click to toggle source

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

  "abc..123"      "ab..789."
+ "ab..789."    + "abc..123"
  ----------      ----------
  "abc.7893"      "abc.7123"
# File lib/strmask.rb, line 119
def +(other)
  other = convert(other)
  i = 0
  o = ''
  while i < to_str.size
    if other[i,1] == ESC
      o << to_str[i,1]
    else
      o << other[i,1]
    end
    i += 1
  end
  self.class.new(o, @re)
end
Also aliased as: |
-(other) click to toggle source

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"
# File lib/strmask.rb, line 95
def -(other)
  other = convert(other)
  i = 0
  o = ''
  while i < to_str.size
    if to_str[i,1] == other[i,1]
      o << ESC
    else
      o << other[i,1]
    end
    i += 1
  end
  self.class.new(o, @re)
end
==(other) click to toggle source
# File lib/strmask.rb, line 210
def ==(other)
  case other
  when Mask
    to_str == other.to_str
  else
    to_str == other.to_s
  end
end
[](*a) click to toggle source
# File lib/strmask.rb, line 67
def [](*a)
  to_str[*a]
end
^(other) click to toggle source

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"
# File lib/strmask.rb, line 192
def ^(other)
  other = convert(other)
  i = 0
  o = ''
  while i < to_str.size
    if to_str[i,1] == ESC
      o << other[i,1]
    elsif other[i,1] == ESC
      o << to_str[i,1]
    else
      o << ESC
    end
    i += 1
  end
  self.class.new(o, @re)
end
apply(s=nil, *a, &b) click to toggle source

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

# File lib/strmask.rb, line 221
def apply(s=nil, *a, &b)
  if s
    to_str.send(s,*a,&b).to_mask
  else
    @_self ||= Functor.new do |op, *a|
      to_str.send(op,*a).to_mask
    end
  end
end
inspect() click to toggle source
# File lib/strmask.rb, line 62
def inspect
  @to_str.gsub(ESC, @re).inspect
end
mask(re) click to toggle source

Create a new mask with the same underlying string, but using a different empty slot.

@param [String] re

Single character string used to mark empty slots.
# File lib/strmask.rb, line 77
def mask(re)
  self.class.new(to_str,re)
end
mask!(re) click to toggle source
# File lib/strmask.rb, line 82
def mask!(re)
  to_str.gsub!(re){ |s| ESC * s.size }
end
method_missing(s, *a, &b) click to toggle source

Delegate any missing methods to underlying string.

Calls superclass method
# File lib/strmask.rb, line 255
def method_missing(s, *a, &b)
  begin
    to_str.send(s, *a, &b)
  rescue NoMethodError
    super(s, *a, &b)
  end
end
replace(string) click to toggle source
# File lib/strmask.rb, line 232
def replace(string)
  @to_str = string.to_s
end
to_s() click to toggle source

TODO: Should this use the escape character or not?

# File lib/strmask.rb, line 57
def to_s
  @to_str.gsub(ESC, @re)
end
to_str() click to toggle source

The underlying string object.

# File lib/strmask.rb, line 52
def to_str
  @to_str
end
|(other)

Mask OR is the same as ADD.

Alias for: +

Private Instance Methods

convert(other) click to toggle source
# File lib/strmask.rb, line 265
def convert(other)
  case other
  when Mask
    other
  else
    self.class.new(other.to_s, @re)
  end
end