Class: PatternMatch::ValuePattern

Inherits:
PatternElement show all
Defined in:
lib/egison/core.rb

Defined Under Namespace

Classes: BindingModule

Instance Attribute Summary

Attributes inherited from Pattern

#quantified

Instance Method Summary collapse

Methods inherited from Pattern

#match_stream, #to_a

Constructor Details

#initialize(ctx, expr) ⇒ ValuePattern

Returns a new instance of ValuePattern.



271
272
273
274
275
# File 'lib/egison/core.rb', line 271

def initialize(ctx, expr)
  super()
  @ctx = ctx
  @expr = expr
end

Instance Method Details

#binding_module(obj) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/egison/core.rb', line 312

def binding_module(obj)
  m = obj.singleton_class.ancestors.find { |i| i.kind_of?(BindingModule) }
  unless m
    m = BindingModule.new
    obj.singleton_class.class_eval do
      if respond_to?(:prepend, true)
        prepend m
      else
        include m
      end
    end
  end
  m
end

#match(tgt, bindings) ⇒ Object



277
278
279
280
281
282
283
284
# File 'lib/egison/core.rb', line 277

def match(tgt, bindings)
  val = with_bindings(@ctx, bindings, {:expr => @expr}) { eval expr }
  if val.__send__(:===, tgt)
    [[[], []]]
  else
    []
  end
end

#with_bindings(obj, bindings, ext_bindings, &block) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/egison/core.rb', line 289

def with_bindings(obj, bindings, ext_bindings, &block)
  binding_module(obj).module_eval do
    begin
      bindings.each do |name, val|
        define_method(name) { val }
        private name
      end
      ext_bindings.each do |name, val|
        define_method(name) { val }
        private name
      end
      obj.instance_eval(&block)
    ensure
      bindings.each do |name, _|
        remove_method(name)
      end
      ext_bindings.each do |name, _|
        remove_method(name)
      end
    end
  end
end