Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / abs.rb
1 #
2 # abs.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:abs, :type => :rvalue, :doc => <<-DOC
6     Returns the absolute value of a number, for example -34.56 becomes
7     34.56. Takes a single integer and float value as an argument.
8
9     Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
10     will be used instead of this function.
11     DOC
12              ) do |arguments|
13
14     raise(Puppet::ParseError, "abs(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
15
16     value = arguments[0]
17
18     # Numbers in Puppet are often string-encoded which is troublesome ...
19     if value.is_a?(String)
20       if value =~ %r{^-?(?:\d+)(?:\.\d+){1}$}
21         value = value.to_f
22       elsif value =~ %r{^-?\d+$}
23         value = value.to_i
24       else
25         raise(Puppet::ParseError, 'abs(): Requires float or integer to work with')
26       end
27     end
28
29     # We have numeric value to handle ...
30     result = value.abs
31
32     return result
33   end
34 end
35
36 # vim: set ts=2 sw=2 et :