Class: PatternMatch::ValuePattern
- Inherits:
-
PatternElement
- Object
- Pattern
- PatternElement
- PatternMatch::ValuePattern
- Defined in:
- lib/egison/core.rb
Defined Under Namespace
Classes: BindingModule
Instance Attribute Summary
Attributes inherited from Pattern
Instance Method Summary collapse
- #binding_module(obj) ⇒ Object
-
#initialize(ctx, expr) ⇒ ValuePattern
constructor
A new instance of ValuePattern.
- #match(tgt, bindings) ⇒ Object
- #with_bindings(obj, bindings, ext_bindings, &block) ⇒ Object
Methods inherited from Pattern
Constructor Details
#initialize(ctx, expr) ⇒ ValuePattern
Returns a new instance of ValuePattern.
246 247 248 249 250 |
# File 'lib/egison/core.rb', line 246 def initialize(ctx, expr) super() @ctx = ctx @expr = expr end |
Instance Method Details
#binding_module(obj) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/egison/core.rb', line 287 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
252 253 254 255 256 257 258 259 |
# File 'lib/egison/core.rb', line 252 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
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/egison/core.rb', line 264 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 |