Puppet Function: bool2num

Defined in:
vendor_modules/stdlib/lib/puppet/parser/functions/bool2num.rb
Function type:
Ruby 3.x API

Summary

Converts a boolean to a number.

Overview

bool2num()Integer

Converts the values:

```
false, f, 0, n, and no to 0
true, t, 1, y, and yes to 1
```

Requires a single boolean or string as an input.

> Note:

since Puppet 5.0.0 it is possible to create new data types for almost any
datatype using the type system and the built-in
[`Numeric.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-numeric),
[`Integer.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-integer), and
[`Float.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-float)
function are used to convert to numeric values.
```
notice(Integer(false)) # Notices 0
notice(Float(true))    # Notices 1.0
```

Returns:

  • (Integer)

    The converted value as a number



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
41
# File 'vendor_modules/stdlib/lib/puppet/parser/functions/bool2num.rb', line 7

newfunction(:bool2num, type: :rvalue, doc: <<-DOC
  @summary
    Converts a boolean to a number.

  Converts the values:
    ```
    false, f, 0, n, and no to 0
    true, t, 1, y, and yes to 1
    ```
  Requires a single boolean or string as an input.

  > *Note:*
    since Puppet 5.0.0 it is possible to create new data types for almost any
    datatype using the type system and the built-in
    [`Numeric.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-numeric),
    [`Integer.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-integer), and
    [`Float.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-float)
    function are used to convert to numeric values.
    ```
    notice(Integer(false)) # Notices 0
    notice(Float(true))    # Notices 1.0
    ```

  @return [Integer] The converted value as a number
  DOC
) do |arguments|
  raise(Puppet::ParseError, "bool2num(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?

  value = function_str2bool([arguments[0]])

  # We have real boolean values as well ...
  result = value ? 1 : 0

  return result
end