Class: Rjq::AST::UnaryOp
Instance Attribute Summary collapse
-
#expression ⇒ Object
readonly
Returns the value of attribute expression.
-
#op ⇒ Object
readonly
Returns the value of attribute op.
Attributes inherited from Node
Instance Method Summary collapse
- #eval(input, context) ⇒ Object
-
#initialize(op, expression) ⇒ UnaryOp
constructor
A new instance of UnaryOp.
Methods inherited from Node
#assign_value, #invalid_path_message, #paths, #take, #with_source_span
Constructor Details
#initialize(op, expression) ⇒ UnaryOp
Returns a new instance of UnaryOp.
787 788 789 790 |
# File 'lib/rjq/ast.rb', line 787 def initialize(op, expression) @op = op @expression = expression end |
Instance Attribute Details
#expression ⇒ Object (readonly)
Returns the value of attribute expression.
785 786 787 |
# File 'lib/rjq/ast.rb', line 785 def expression @expression end |
#op ⇒ Object (readonly)
Returns the value of attribute op.
785 786 787 |
# File 'lib/rjq/ast.rb', line 785 def op @op end |
Instance Method Details
#eval(input, context) ⇒ Object
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
# File 'lib/rjq/ast.rb', line 792 def eval(input, context) @expression.eval(input, context).map do |value| case @op when '-' unless value.is_a?(Numeric) raise TypeError, "#{Value.type_of(value)} (#{AST.short_dump(value)}) cannot be negated" end next -0.0 if value.zero? value * -1 when 'not' !Value.truthy?(value) else raise "unknown unary operator #{@op}" end end end |