Sha256: e44ea8a5e5c95a56a2954ca615d0bdd278ad49608bc5b608c685fc80f90a377a

Contents?: true

Size: 1.67 KB

Versions: 20

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true
# Strips trailing spaces from a String
#
# This function is compatible with the stdlib function with the same name.
#
# The function does the following:
# * For a `String` the conversion removes all trailing ASCII white space characters such as space, tab, newline, and return.
#   It does not remove other space-like characters like hard space (Unicode U+00A0). (Tip, `/^[[:space:]]/` regular expression
#   matches all space-like characters).
# * 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 trailing space from a String
# ```puppet
# " hello\n\t".rstrip()
# rstrip(" hello\n\t")
# ```
# Would both result in `"hello"`
#
# @example Removing trailing space from strings in an Array
# ```puppet
# [" hello\n\t", " hi\n\t"].rstrip()
# rstrip([" hello\n\t", " hi\n\t"])
# ```
# Would both result in `['hello', 'hi']`
#
Puppet::Functions.create_function(:rstrip) 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.rstrip
  end

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

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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
puppet-8.3.0 lib/puppet/functions/rstrip.rb
puppet-8.3.0-x86-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.3.0-x64-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.3.0-universal-darwin lib/puppet/functions/rstrip.rb
puppet-8.3.1 lib/puppet/functions/rstrip.rb
puppet-8.3.1-x86-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.3.1-x64-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.3.1-universal-darwin lib/puppet/functions/rstrip.rb
puppet-8.2.0 lib/puppet/functions/rstrip.rb
puppet-8.2.0-x86-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.2.0-x64-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.2.0-universal-darwin lib/puppet/functions/rstrip.rb
puppet-8.1.0 lib/puppet/functions/rstrip.rb
puppet-8.1.0-x86-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.1.0-x64-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.1.0-universal-darwin lib/puppet/functions/rstrip.rb
puppet-8.0.1 lib/puppet/functions/rstrip.rb
puppet-8.0.1-x86-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.0.1-x64-mingw32 lib/puppet/functions/rstrip.rb
puppet-8.0.1-universal-darwin lib/puppet/functions/rstrip.rb