Class: Keisan::Functions::ToH
Instance Attribute Summary
#arity, #name
Instance Method Summary
collapse
#differentiate, #unbound_variables
Constructor Details
#initialize ⇒ ToH
Returns a new instance of ToH.
4
5
6
|
# File 'lib/keisan/functions/to_h.rb', line 4
def initialize
super("to_h", 1)
end
|
Instance Method Details
#evaluate(ast_function, context = nil) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/keisan/functions/to_h.rb', line 13
def evaluate(ast_function, context = nil)
validate_arguments!(ast_function.children.count)
context ||= Context.new
child = ast_function.children[0].simplify(context)
case child
when AST::List
AST::Hash.new(child.children)
when AST::Hash
child
else
raise Exceptions::InvalidFunctionError.new("Cannot call to_h on a #{child.class}")
end
end
|
#simplify(ast_function, context = nil) ⇒ Object
29
30
31
|
# File 'lib/keisan/functions/to_h.rb', line 29
def simplify(ast_function, context = nil)
evaluate(ast_function, context)
end
|
#value(ast_function, context = nil) ⇒ Object
8
9
10
11
|
# File 'lib/keisan/functions/to_h.rb', line 8
def value(ast_function, context = nil)
validate_arguments!(ast_function.children.count)
evaluate(ast_function, context).value(context)
end
|