Sha256: 5f8c103ca901959c4297f0d0ae5a284f593cd6a9b0a54b301c61fcf91b982a40

Contents?: true

Size: 950 Bytes

Versions: 20

Compression:

Stored size: 950 Bytes

Contents

# frozen_string_literal: true
# Returns the length of an Array, Hash, String, or Binary value.
#
# The returned value is a positive integer indicating the number
# of elements in the container; counting (possibly multibyte) characters for a `String`,
# bytes in a `Binary`, number of elements in an `Array`, and number of
# key-value associations in a Hash.
#
# @example Using `length`
#
# ```puppet
# "roses".length()        # 5
# length("violets")       # 7
# [10, 20].length         # 2
# {a => 1, b => 3}.length # 2
# ```
#
# @since 5.5.0 - also supporting Binary
#
Puppet::Functions.create_function(:length) do
  dispatch :collection_length do
    param 'Collection', :arg
  end

  dispatch :string_length do
    param 'String', :arg
  end

  dispatch :binary_length do
    param 'Binary', :arg
  end

  def collection_length(col)
    col.size
  end

  def string_length(s)
    s.length
  end

  def binary_length(bin)
    bin.length
  end

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
puppet-8.3.0 lib/puppet/functions/length.rb
puppet-8.3.0-x86-mingw32 lib/puppet/functions/length.rb
puppet-8.3.0-x64-mingw32 lib/puppet/functions/length.rb
puppet-8.3.0-universal-darwin lib/puppet/functions/length.rb
puppet-8.3.1 lib/puppet/functions/length.rb
puppet-8.3.1-x86-mingw32 lib/puppet/functions/length.rb
puppet-8.3.1-x64-mingw32 lib/puppet/functions/length.rb
puppet-8.3.1-universal-darwin lib/puppet/functions/length.rb
puppet-8.2.0 lib/puppet/functions/length.rb
puppet-8.2.0-x86-mingw32 lib/puppet/functions/length.rb
puppet-8.2.0-x64-mingw32 lib/puppet/functions/length.rb
puppet-8.2.0-universal-darwin lib/puppet/functions/length.rb
puppet-8.1.0 lib/puppet/functions/length.rb
puppet-8.1.0-x86-mingw32 lib/puppet/functions/length.rb
puppet-8.1.0-x64-mingw32 lib/puppet/functions/length.rb
puppet-8.1.0-universal-darwin lib/puppet/functions/length.rb
puppet-8.0.1 lib/puppet/functions/length.rb
puppet-8.0.1-x86-mingw32 lib/puppet/functions/length.rb
puppet-8.0.1-x64-mingw32 lib/puppet/functions/length.rb
puppet-8.0.1-universal-darwin lib/puppet/functions/length.rb