Method: DMatch::Env#bind

Defined in:
lib/destructure/env.rb

#bind(identifier, value) ⇒ Object Also known as: []=



22
23
24
25
26
27
28
29
30
31
# File 'lib/destructure/env.rb', line 22

def bind(identifier, value)
  raise 'identifier must be a Var' unless identifier.is_a? Var
  value_to_store = value.nil? ? EnvNil.new : value
  existing_key = env.keys.select{|k| k == identifier || (k.name.is_a?(Symbol) && k.name == identifier.name)}.first
  return nil if existing_key &&
      (DMatch.match(env[existing_key], value_to_store).nil? ||
      DMatch.match(value_to_store, env[existing_key]).nil?)
  env[existing_key || identifier] = value_to_store
  self
end