Puppet Function: lead

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

Overview

lead()Any

This takes an input of an integer, and returns it to a string with leading zeroes.

Returns:

  • (Any)


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

newfunction(:lead, :type => :rvalue, :doc => <<-EOS
This takes an input of an integer, and returns it to a string with leading
zeroes.
EOS
) do |arguments|

  raise(Puppet::ParseError, "lead(): Wrong number of arguments " +
    "given (#{arguments.size} for 2)") if arguments.size < 2

  value = Integer(arguments[0])
  width = Integer(arguments[1])

  unless value.is_a?(Integer)
    raise(Puppet::ParseError, 'lead(): expected integer for value parameter')
  end

  unless width.is_a?(Integer)
    raise(Puppet::ParseError, 'lead(): expected integer for width parameter')
  end


  result = "%0#{width}i" % value

  if result.size > width
    raise(Puppet::ParseError, 'lead(): passed in value is longer than width' )
  end

  return result
end