]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/chop.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / chop.rb
1 #
2 #  chop.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:chop, :type => :rvalue, :doc => <<-'EOS'
7     Returns a new string with the last character removed. If the string ends
8     with `\r\n`, both characters are removed. Applying chop to an empty
9     string returns an empty string. If you wish to merely remove record
10     separators then you should use the `chomp` function.
11     Requires a string or array of strings as input.
12     EOS
13   ) do |arguments|
14
15     raise(Puppet::ParseError, "chop(): Wrong number of arguments " +
16       "given (#{arguments.size} for 1)") if arguments.size < 1
17
18     value = arguments[0]
19
20     unless value.is_a?(Array) || value.is_a?(String)
21       raise(Puppet::ParseError, 'chop(): Requires either an ' +
22         'array or string to work with')
23     end
24
25     if value.is_a?(Array)
26       # Numbers in Puppet are often string-encoded which is troublesome ...
27       result = value.collect { |i| i.is_a?(String) ? i.chop : i }
28     else
29       result = value.chop
30     end
31
32     return result
33   end
34 end
35
36 # vim: set ts=2 sw=2 et :