-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
abs.rb
64 lines (59 loc) · 2.12 KB
/
abs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
# Returns the absolute value of a Numeric value, for example -34.56 becomes
# 34.56. Takes a single `Integer` or `Float` value as an argument.
#
# *Deprecated behavior*
#
# For backwards compatibility reasons this function also works when given a
# number in `String` format such that it first attempts to covert it to either a `Float` or
# an `Integer` and then taking the absolute value of the result. Only strings representing
# a number in decimal format is supported - an error is raised if
# value is not decimal (using base 10). Leading 0 chars in the string
# are ignored. A floating point value in string form can use some forms of
# scientific notation but not all.
#
# Callers should convert strings to `Numeric` before calling
# this function to have full control over the conversion.
#
# @example Converting to Numeric before calling
# ```puppet
# abs(Numeric($str_val))
# ```
#
# It is worth noting that `Numeric` can convert to absolute value
# directly as in the following examples:
#
# @example Absolute value and String to Numeric
# ```puppet
# Numeric($strval, true) # Converts to absolute Integer or Float
# Integer($strval, 10, true) # Converts to absolute Integer using base 10 (decimal)
# Integer($strval, 16, true) # Converts to absolute Integer using base 16 (hex)
# Float($strval, true) # Converts to absolute Float
# ```
#
Puppet::Functions.create_function(:abs) do
dispatch :on_numeric do
param 'Numeric', :val
end
dispatch :on_string do
param 'String', :val
end
def on_numeric(x)
x.abs
end
def on_string(x)
Puppet.warn_once('deprecations', 'abs_function_numeric_coerce_string',
_("The abs() function's auto conversion of String to Numeric is deprecated - change to convert input before calling"))
# These patterns for conversion are backwards compatible with the stdlib
# version of this function.
#
case x
when /^-?(?:\d+)(?:\.\d+){1}$/
x.to_f.abs
when /^-?\d+$/
x.to_i.abs
else
raise(ArgumentError, 'abs(): Requires float or integer to work with - was given non decimal string')
end
end
end