String Masking

require 'strmask'

There are a few ways to create a string mask.

x1 = String::Mask.new("abc..123", '.')
x2 = String::Mask["abc..123", '.']
x3 = "abc..123".mask('.')

All of the above examples are equivelent.

x1.assert == x2
x2.assert == x3
x3.assert == x1

Notice in all these example we specified a dot (‘.’) as the escaping character. Leaving this off defaults the chracter to ASCII ESC (“032”). ASCII ESC is a good choice for real world usage, but for demonstration puposes a dot is clearly much easier to read.

We will use the folowing two string masks to demonstrate the various masking operators below.

x1 = "abc..123".mask('.')
x2 = "ab..789.".mask('.')

For Addition, as long as there is a value other then empty slot the character filters though, with the last string taking precedence.

(x1 + x2)  #=> "abc.7893".mask('.')
(x2 + x1)  #=> "abc.7123".mask('.')

The OR operator is the same as addition.

(x1 | x2)  #=> "abc.7893".mask('.')
(x2 | x1)  #=> "abc.7123".mask('.')

For Subtraction, where the characters are the same, the result is empty, where they differ the result reflects the last string.

(x1 - x2)  #=> "....789.".mask('.')
(x2 - x1)  #=> "..c..123".mask('.')

For Multiplication (Exclusive AND), where the characters are the same the result is the same, where they differ the result reflects the later.

(x1 * x2)  #=> "ab..789.".mask('.')
(x2 * x1)  #=> "abc..123".mask('.')

The AND operator, only slots that are the same (using ==) filter through.

(x1 & x2)  #=> "ab......".mask('.')
(x2 & x1)  #=> "ab......".mask('.')

The XOR operator, only where there is an empty slot will the value filter through.

(x1 ^ x2)  #=> "..c.7..3".mask('.')
(x2 ^ x1)  #=> "..c.7..3".mask('.')