Class: PatternMatching::Bindings::BoundValue

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

Overview

The interesting work of Bindings happens in this class.

Allows setting up guards via the >> oprator.

If guards pass, or no guards have been added to a BoundValue, then comparing via == or === with that BoundValue will always return true, and will save the compared-to value in the hash, presumably provided by an instance of the Bindings class.

Instance Method Summary collapse

Constructor Details

#initialize(bindings, name) ⇒ BoundValue

Returns a new instance of BoundValue.



34
35
36
37
38
# File 'lib/pattern_matching/bindings.rb', line 34

def initialize(bindings, name)
  @bindings = bindings
  @name     = name
  @guards   = []
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
43
44
# File 'lib/pattern_matching/bindings.rb', line 40

def ==(other)
  return false unless @guards.all? { |g| g === other }
  @bindings[@name] = other
  true
end

#===(other) ⇒ Object



46
47
48
49
50
# File 'lib/pattern_matching/bindings.rb', line 46

def ===(other)
  return false unless @guards.all? { |g| g === other }
  @bindings[@name] = other
  true
end

#>>(guard) ⇒ Object



52
53
54
55
# File 'lib/pattern_matching/bindings.rb', line 52

def >>(guard)
  @guards << guard
  self
end