]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/chop.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / 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     klass = value.class
20
21     unless [Array, String].include?(klass)
22       raise(Puppet::ParseError, 'chop(): Requires either an ' +
23         'array or string to work with')
24     end
25
26     if value.is_a?(Array)
27       # Numbers in Puppet are often string-encoded which is troublesome ...
28       result = value.collect { |i| i.is_a?(String) ? i.chop : i }
29     else
30       result = value.chop
31     end
32
33     return result
34   end
35 end
36
37 # vim: set ts=2 sw=2 et :