Module: Trxl::PatternMatchExp1

Defined in:
lib/trxl/trxl_grammar.rb

Instance Method Summary collapse

Instance Method Details

#eval(env = Environment.new) ⇒ Object



4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
# File 'lib/trxl/trxl_grammar.rb', line 4140

def eval(env = Environment.new)
  match_op = elements[1].match_op
  pattern = elements[1].pattern(env)
  enumerable = variable.eval(env)
  if enumerable.is_a?(Array)
    enumerable.find_all { |e| e.send(match_op, pattern) }
  elsif enumerable.is_a?(Hash)
    enumerable.select { |k, v| v.send(match_op, pattern) }
  else
    msg = "Pattern matching is not possible for #{enumerable.class} (only Arrays and Hashes allowed)"
    raise Trxl::InvalidOperationException, msg
  end
end