Puppet Function: difference_within_margin

Defined in:
lib/puppet/parser/functions/difference_within_margin.rb
Function type:
Ruby 3.x API

Overview

difference_within_margin()Any

Get’s the difference between two numbers, with a third argument as a margin

Example:

compare_with_margin(100,150,60)

Would result in:

true

compare_with_margin(100,150,40)

Would result in:

false

Returns:

  • (Any)


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
# File 'lib/puppet/parser/functions/difference_within_margin.rb', line 18

newfunction(:difference_within_margin, type: :rvalue, doc: <<~EOS
  Get's the difference between two numbers, with a third argument as a margin

  *Example:*

      compare_with_margin(100,150,60)

  Would result in:

      true

      compare_with_margin(100,150,40)

  Would result in:

      false

EOS
) do |arguments|
  # Check that more than 2 arguments have been given ...
  unless arguments.size == 2
    raise(Puppet::ParseError, 'compare_with_margin(): Wrong number of arguments ' \
                              "given (#{arguments.size} for 2)")
  end

  # Check that the first parameter is an array
  raise(Puppet::ParseError, 'difference_within_margin(): Requires array to work with') unless arguments[0].is_a?(Array)

  # Check that the first parameter is an array
  raise(Puppet::ParseError, 'difference_within_margin(): arg[0] array cannot be empty') if arguments[0].empty?

  arguments[0].map!(&:to_f)

  difference = arguments[0].minmax[1].to_f - arguments[0].minmax[0].to_f

  return true if difference < arguments[1].to_f

  return false
end