Puppet Function: size

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

Summary

Returns the number of elements in a string, an array or a hash

Overview

size()Any

> Note: that since Puppet 5.4.0, the length() function in Puppet is preferred over this. For versions of Puppet < 5.4.0 use the stdlib length() function.

Returns:

  • (Any)

    the number of elements in a string, an array or a hash



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

newfunction(:size, type: :rvalue, doc: <<-DOC
  @summary
    Returns the number of elements in a string, an array or a hash

  @return
    the number of elements in a string, an array or a hash

  > *Note:* that since Puppet 5.4.0, the length() function in Puppet is preferred over this. For versions
  of Puppet < 5.4.0 use the stdlib length() function.
DOC
) do |arguments|
  raise(Puppet::ParseError, "size(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?

  item = arguments[0]

  function_deprecation([:size, 'This method is going to be deprecated, please use the stdlib length function.'])

  if item.is_a?(String)

    begin
      #
      # Check whether your item is a numeric value or not ...
      # This will take care about positive and/or negative numbers
      # for both integer and floating-point values ...
      #
      # Please note that Puppet has no notion of hexadecimal
      # nor octal numbers for its DSL at this point in time ...
      #
      Float(item)

      raise(Puppet::ParseError, 'size(): Requires either string, array or hash to work with')
    rescue ArgumentError
      result = item.size
    end

  elsif item.is_a?(Array) || item.is_a?(Hash)
    result = item.size
  else
    raise(Puppet::ParseError, 'size(): Unknown type given')
  end

  return result
end