Class: PatternMatch::PatternVariable

Inherits:
PatternElement show all
Defined in:
lib/pattern-match/core.rb,
lib/pattern-match/experimental.rb

Instance Attribute Summary collapse

Attributes inherited from Pattern

#next, #parent, #prev

Instance Method Summary collapse

Methods inherited from PatternElement

#quantifier?

Methods inherited from Pattern

#!@, #&, #ancestors, #append, #directly_quantified?, #quantified?, #quantifier?, #quasibinding, #root, #root?, #to_a, #validate, #|

Methods included from PatternMatch::Pattern::Backtrackable

#choice_points

Constructor Details

#initialize(name) ⇒ PatternVariable

Returns a new instance of PatternVariable.



229
230
231
232
233
234
# File 'lib/pattern-match/core.rb', line 229

def initialize(name)
  super()
  @name = name
  @val = nil
  @bind_to = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



227
228
229
# File 'lib/pattern-match/core.rb', line 227

def name
  @name
end

#valObject (readonly)

Returns the value of attribute val.



227
228
229
# File 'lib/pattern-match/core.rb', line 227

def val
  @val
end

Instance Method Details

#<<(converter) ⇒ Object



164
165
166
167
# File 'lib/pattern-match/experimental.rb', line 164

def <<(converter)
  @converter = converter.respond_to?(:call) ? converter : converter.to_proc
  self
end

#inspectObject



275
276
277
# File 'lib/pattern-match/core.rb', line 275

def inspect
  "#<#{self.class.name}: name=#{name.inspect}, val=#{@val.inspect}>"
end

#match(vals) ⇒ Object



236
237
238
239
240
241
# File 'lib/pattern-match/core.rb', line 236

def match(vals)
  super do |val|
    bind(val)
    true
  end
end

#set_bind_to(quantifier) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/pattern-match/core.rb', line 247

def set_bind_to(quantifier)
  n = nest_level(quantifier)
  if n == 0
    @val = @bind_to = []
  else
    outer = @val
    (n - 1).times do
      outer = outer[-1]
    end
    @bind_to = []
    outer << @bind_to
  end
end

#unset_bind_to(quantifier) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/pattern-match/core.rb', line 261

def unset_bind_to(quantifier)
  n = nest_level(quantifier)
  @bind_to = nil
  if n == 0
    # do nothing
  else
    outer = @val
    (n - 1).times do
      outer = outer[-1]
    end
    outer.pop
  end
end

#varsObject



243
244
245
# File 'lib/pattern-match/core.rb', line 243

def vars
  [self]
end