Puppet Function: member

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

Overview

member()Any

This function determines if a variable is a member of an array. The variable can be a string, fixnum, or array.

Examples:

member(['a','b'], 'b')

Would return: true

member(['a', 'b', 'c'], ['a', 'b'])

would return: true

member(['a','b'], 'c')

Would return: false

member(['a', 'b', 'c'], ['d', 'b'])

would return: false

Returns:

  • (Any)


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
50
51
52
53
54
55
56
57
58
59
# File 'puppet/modules/stdlib/lib/puppet/parser/functions/member.rb', line 9

newfunction(:member, :type => :rvalue, :doc => <<-EOS
This function determines if a variable is a member of an array.
The variable can be a string, fixnum, or array.

*Examples:*

  member(['a','b'], 'b')

Would return: true

  member(['a', 'b', 'c'], ['a', 'b'])

would return: true

  member(['a','b'], 'c')

Would return: false

  member(['a', 'b', 'c'], ['d', 'b'])

would return: false
  EOS
) do |arguments|

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

  array = arguments[0]

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

  unless arguments[1].is_a? String or arguments[1].is_a? Fixnum or arguments[1].is_a? Array
    raise(Puppet::ParseError, 'member(): Item to search for must be a string, fixnum, or array')
  end

  if arguments[1].is_a? String or arguments[1].is_a? Fixnum
    item = [arguments[1]]
  else
    item = arguments[1]
  end


  raise(Puppet::ParseError, 'member(): You must provide item ' +
    'to search for within array given') if item.respond_to?('empty?') && item.empty?

  result = (item - array).empty?

  return result
end