Class: Dentaku::AST::RubyMath

Inherits:
Function show all
Defined in:
lib/dentaku/ast/functions/ruby_math.rb

Constant Summary collapse

ARRAY_RETURN_TYPES =
[:frexp, :lgamma].freeze

Constants inherited from Function

Function::DIG

Constants inherited from Node

Node::STATIC_CONTEXT, Node::STATIC_MODE_KEY

Instance Attribute Summary

Attributes inherited from Function

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Function

#accept, #dependencies, get, #initialize, register, register_class, registry, volatile?

Methods inherited from Node

#dependencies, #name, precedence, #pure?, resolve_class

Constructor Details

This class inherits a constructor from Dentaku::AST::Function

Class Method Details

.[](method) ⇒ Object



7
8
9
10
11
12
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 7

def self.[](method)
  klass_name = method.to_s.capitalize
  klass = const_set(klass_name , Class.new(self))
  klass.implement(method)
  const_get(klass_name)
end

.arityObject



26
27
28
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 26

def self.arity
  @implementation.arity < 0 ? nil : @implementation.arity
end

.call(*args) ⇒ Object



38
39
40
41
42
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 38

def self.call(*args)
  @implementation.call(*args)
rescue Math::DomainError => _e
  raise Dentaku::MathDomainError.new(name, args)
end

.implement(method) ⇒ Object



14
15
16
17
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 14

def self.implement(method)
  @name = method
  @implementation = Math.method(method)
end

.max_param_countObject



34
35
36
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 34

def self.max_param_count
  @implementation.parameters.select { |type, _name| type == :rest }.any? ? Float::INFINITY : @implementation.parameters.count
end

.min_param_countObject



30
31
32
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 30

def self.min_param_count
  @implementation.parameters.select { |type, _name| type == :req }.count
end

.nameObject

Ruby documents Module#name as a String (or nil for anonymous classes), so expose one even though the function is registered under a Symbol



22
23
24
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 22

def self.name
  @name.to_s
end

Instance Method Details

#typeObject



51
52
53
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 51

def type
  ARRAY_RETURN_TYPES.include?(@name) ? :array : :numeric
end

#value(context = {}) ⇒ Object



44
45
46
47
# File 'lib/dentaku/ast/functions/ruby_math.rb', line 44

def value(context = {})
  args = @args.flatten.map { |a|Dentaku::NumericParser.ensure_numeric!(a.value(context)) }
  self.class.call(*args)
end