chop.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #
  2. # chop.rb
  3. #
  4. module Puppet::Parser::Functions
  5. newfunction(:chop, :type => :rvalue, :doc => <<-'EOS'
  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. EOS
  12. ) do |arguments|
  13. raise(Puppet::ParseError, "chop(): Wrong number of arguments " +
  14. "given (#{arguments.size} for 1)") if arguments.size < 1
  15. value = arguments[0]
  16. klass = value.class
  17. unless [Array, String].include?(klass)
  18. raise(Puppet::ParseError, 'chop(): Requires either an ' +
  19. 'array or string to work with')
  20. end
  21. if value.is_a?(Array)
  22. # Numbers in Puppet are often string-encoded which is troublesome ...
  23. result = value.collect { |i| i.is_a?(String) ? i.chop : i }
  24. else
  25. result = value.chop
  26. end
  27. return result
  28. end
  29. end
  30. # vim: set ts=2 sw=2 et :