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