Sha256: 4a6766b58d67f14529dcd40fb4bd1c8f0afab411f65dc8a5a3229a4842e63d9e

Contents?: true

Size: 860 Bytes

Versions: 41

Compression:

Stored size: 860 Bytes

Contents

#
# hash.rb
#

module Puppet::Parser::Functions
  newfunction(:hash, :type => :rvalue, :doc => <<-EOS
This function converts an array into a hash.

*Examples:*

    hash(['a',1,'b',2,'c',3])

Would return: {'a'=>1,'b'=>2,'c'=>3}
    EOS
  ) do |arguments|

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

    array = arguments[0]

    unless array.is_a?(Array)
      raise(Puppet::ParseError, 'hash(): Requires array to work with')
    end

    result = {}

    begin
      # This is to make it compatible with older version of Ruby ...
      array  = array.flatten
      result = Hash[*array]
    rescue Exception
      raise(Puppet::ParseError, 'hash(): Unable to compute ' +
        'hash from array given')
    end

    return result
  end
end

# vim: set ts=2 sw=2 et :

Version data entries

41 entries across 41 versions & 3 rubygems

Version Path
puppet-retrospec-1.8.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.7.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.6.1 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.6.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.5.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.4.1 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.4.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.3.2 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.3.1 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.3.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.2.1 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.2.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.1.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-1.0.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-0.12.1 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-0.12.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-0.11.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-0.10.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-0.9.1 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb
puppet-retrospec-0.9.0 spec/fixtures/modules/stdlib/lib/puppet/parser/functions/hash.rb