source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/delete.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 7 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 1.1 KB
Line 
1#
2# delete.rb
3#
4
5module Puppet::Parser::Functions
6 newfunction(:delete, :type => :rvalue, :doc => <<-EOS
7Deletes all instances of a given element from an array, substring from a
8string, or key from a hash.
9
10*Examples:*
11
12 delete(['a','b','c','b'], 'b')
13 Would return: ['a','c']
14
15 delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
16 Would return: {'a'=>1,'c'=>3}
17
18 delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c'])
19 Would return: {'a'=>1}
20
21 delete('abracadabra', 'bra')
22 Would return: 'acada'
23 EOS
24 ) do |arguments|
25
26 raise(Puppet::ParseError, "delete(): Wrong number of arguments "+
27 "given #{arguments.size} for 2") unless arguments.size == 2
28
29 collection = arguments[0].dup
30 Array(arguments[1]).each do |item|
31 case collection
32 when Array, Hash
33 collection.delete item
34 when String
35 collection.gsub! item, ''
36 else
37 raise(TypeError, "delete(): First argument must be an Array, " +
38 "String, or Hash. Given an argument of class #{collection.class}.")
39 end
40 end
41 collection
42 end
43end
44
45# vim: set ts=2 sw=2 et :
Note: See TracBrowser for help on using the repository browser.