Class: String::Mask

Inherits:
Object
  • Object
show all
Defined in:
lib/strmask.rb

Overview

Mask

Constant Summary collapse

ESC =

ASCII SUBSTITUTE

"\032"

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a, &b) ⇒ Object

Delegate any missing methods to underlying string.



230
231
232
233
234
235
236
# File 'lib/strmask.rb', line 230

def method_missing(s, *a, &b)
  begin
    to_str.send(s, *a, &b)
  rescue NoMethodError
    super(s, *a, &b)
  end
end

Class Method Details

.[](string, re = nil) ⇒ Object



16
17
18
# File 'lib/strmask.rb', line 16

def self.[](string, re=nil)
  new(string, re)
end

Instance Method Details

#&(other) ⇒ Object

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

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


149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/strmask.rb', line 149

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)
end

#*(other) ⇒ Object

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"


126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/strmask.rb', line 126

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)
end

#+(other) ⇒ Object Also known as: |

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"


99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/strmask.rb', line 99

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)
end

#-(other) ⇒ Object

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"


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/strmask.rb', line 75

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)
end

#==(other) ⇒ Object



190
191
192
193
194
195
196
197
# File 'lib/strmask.rb', line 190

def ==(other)
  case other
  when Mask
    to_str == other.to_str
  else
    to_str == other.to_s
  end
end

#[](*a) ⇒ Object



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

def [](*a)
  to_str[*a]
end

#^(other) ⇒ Object

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"


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/strmask.rb', line 172

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)
end

#apply(s = nil, *a, &b) ⇒ Object

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



201
202
203
204
205
206
207
208
209
# File 'lib/strmask.rb', line 201

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

#inspectObject



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

def inspect
  to_str.inspect
end

#mask(re) ⇒ Object



58
59
60
# File 'lib/strmask.rb', line 58

def mask(re)
  self.class.new(to_str,re)
end

#mask!(re) ⇒ Object



62
63
64
# File 'lib/strmask.rb', line 62

def mask!(re)
  to_str.gsub!(re){ |s| ESC * s.size }
end

#replace(string) ⇒ Object



212
213
214
# File 'lib/strmask.rb', line 212

def replace(string)
  @to_str = string.to_s
end

#to_sObject



44
45
46
# File 'lib/strmask.rb', line 44

def to_s
  to_str
end

#to_strObject

The underlying string object.



39
40
41
# File 'lib/strmask.rb', line 39

def to_str
  @to_str
end