Class: JMESPath::Nodes::AvgFunction Private

Inherits:
Function show all
Defined in:
lib/jmespath/nodes/function.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

API:

  • private

Constant Summary

Constants inherited from Function

Function::FUNCTIONS

Instance Method Summary collapse

Methods inherited from Function

create, #initialize, #optimize, #visit

Methods inherited from Node

#chains_with?, #hash_like?, #optimize, #visit

Constructor Details

This class inherits a constructor from JMESPath::Nodes::Function

Instance Method Details

#call(args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jmespath/nodes/function.rb', line 103

def call(args)
  if args.count == 1
    values = args.first
  else
    return maybe_raise Errors::InvalidArityError, "function avg() expects one argument"
  end
  if Array === values
    values.inject(0) do |total,n|
      if Numeric === n
        total + n
      else
        return maybe_raise Errors::InvalidTypeError, "function avg() expects numeric values"
      end
    end / values.size.to_f
  else
    return maybe_raise Errors::InvalidTypeError, "function avg() expects a number"
  end
end