Sha256: 8bc4a9a9620c578a23412fe9f16f3bcf6f71ba2b099bdffd54b5aa52b3ce46ca

Contents?: true

Size: 1.11 KB

Versions: 38

Compression:

Stored size: 1.11 KB

Contents

require 'scanf'

Puppet::Parser::Functions::newfunction(
  :scanf,
  :type => :rvalue,
  :arity => 2,
  :doc => <<-DOC
Scans a string and returns an array of one or more converted values based on the given format string.
See the documenation of Ruby's String#scanf method for details about the supported formats (which
are similar but not identical to the formats used in Puppet's `sprintf` function.)

This function takes two mandatory arguments: the first is the string to convert, and the second is
the format string. The result of the scan is an array, with each sucessfully scanned and transformed value.
The scanning stops if a scan is unsuccesful, and the scanned result up to that point is returned. If there
was no succesful scan, the result is an empty array.

   "42".scanf("%i")

You can also optionally pass a lambda to scanf, to do additional validation or processing.

    "42".scanf("%i") |$x| {
      unless $x[0] =~ Integer {
        fail "Expected a well formed integer value, got '$x[0]'"
      }
      $x[0]
    }
- since 4.0.0
DOC
) do |args|
  data = args[0]
  format = args[1]
  result = data.scanf(format)
end

Version data entries

38 entries across 38 versions & 2 rubygems

Version Path
puppet-retrospec-1.5.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-retrospec-1.4.1 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-retrospec-1.4.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-retrospec-1.3.2 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-retrospec-1.3.1 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-retrospec-1.3.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-retrospec-1.2.1 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-retrospec-1.2.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-retrospec-1.1.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-4.5.3 lib/puppet/parser/functions/scanf.rb
puppet-4.5.3-x86-mingw32 lib/puppet/parser/functions/scanf.rb
puppet-4.5.3-x64-mingw32 lib/puppet/parser/functions/scanf.rb
puppet-4.5.3-universal-darwin lib/puppet/parser/functions/scanf.rb
puppet-retrospec-1.0.0 vendor/gems/puppet-4.5.2/lib/puppet/parser/functions/scanf.rb
puppet-4.5.2 lib/puppet/parser/functions/scanf.rb
puppet-4.5.2-x86-mingw32 lib/puppet/parser/functions/scanf.rb
puppet-4.5.2-x64-mingw32 lib/puppet/parser/functions/scanf.rb
puppet-4.5.2-universal-darwin lib/puppet/parser/functions/scanf.rb
puppet-4.5.1 lib/puppet/parser/functions/scanf.rb
puppet-4.5.1-x86-mingw32 lib/puppet/parser/functions/scanf.rb