File: scanf.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (40 lines) | stat: -rw-r--r-- 1,244 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

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 documentation 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 successfully scanned and transformed value.
    The scanning stops if a scan is unsuccessful, and the scanned result up to that point is returned. If there
    was no successful scan, the result is an empty array.

    ```puppet
    "42".scanf("%i")
    ```

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

    ```puppet
    "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]
  data.scanf(format)
end