HEX
Server: Apache
System: Linux dinesh8189 5.15.98-grsec-sharedvalley-2.lc.el8.x86_64 #1 SMP Thu Mar 9 09:07:30 -03 2023 x86_64
User: cgmgerenciamento1 (814285)
PHP: 8.1.26
Disabled: apache_child_terminate,dl,escapeshellarg,escapeshellcmd,exec,link,mail,openlog,passthru,pcntl_alarm,pcntl_exec,pcntl_fork,pcntl_get_last_error,pcntl_getpriority,pcntl_setpriority,pcntl_signal,pcntl_signal_dispatch,pcntl_sigprocmask,pcntl_sigtimedwait,pcntl_sigwaitinfo,pcntl_strerror,pcntl_wait,pcntl_waitpid,pcntl_wexitstatus,pcntl_wifexited,pcntl_wifsignaled,pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,php_check_syntax,php_strip_whitespace,popen,proc_close,proc_open,shell_exec,symlink,system
Upload Files
File: //opt/puppetlabs/puppet/lib/ruby/vendor_ruby/puppet/functions/chomp.rb
# Returns a new string with the record separator character(s) removed.
# The record separator is the line ending characters `\r` and `\n`.
#
# This function is compatible with the stdlib function with the same name.
#
# The function does the following:
# * For a `String` the conversion removes `\r\n`, `\n` or `\r` from the end of a string.
# * For an `Iterable[Variant[String, Numeric]]` (for example an `Array`) each value is processed and the conversion is not recursive.
# * If the value is `Numeric` it is simply returned (this is for backwards compatibility).
# * An error is raised for all other data types.
#
# @example Removing line endings
# ```puppet
# "hello\r\n".chomp()
# chomp("hello\r\n")
# ```
# Would both result in `"hello"`
#
# @example Removing line endings in an array
# ```puppet
# ["hello\r\n", "hi\r\n"].chomp()
# chomp(["hello\r\n", "hi\r\n"])
# ```
# Would both result in `['hello', 'hi']`
#
Puppet::Functions.create_function(:chomp) do

  dispatch :on_numeric do
    param 'Numeric', :arg
  end

  dispatch :on_string do
    param 'String', :arg
  end

  dispatch :on_iterable do
    param 'Iterable[Variant[String, Numeric]]', :arg
  end

  # unit function - since the old implementation skipped Numeric values
  def on_numeric(n)
    n
  end

  def on_string(s)
    s.chomp
  end

  def on_iterable(a)
    a.map {|x| do_chomp(x) }
  end

  def do_chomp(x)
    # x can only be a String or Numeric because type constraints have been automatically applied
    x.is_a?(String) ? x.chomp : x
  end
end