4 module Puppet::Parser::Functions
5 newfunction(:bool2num, :type => :rvalue, :doc => <<-DOC
6 Converts a boolean to a number. Converts the values:
7 false, f, 0, n, and no to 0
8 true, t, 1, y, and yes to 1
9 Requires a single boolean or string as an input.
11 Note that since Puppet 5.0.0 it is possible to create new data types for almost any
12 datatype using the type system and the built-in
13 [`Numeric.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-numeric),
14 [`Integer.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-integer), and
15 [`Float.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-float)
16 function are used to convert to numeric values.
18 notice(Integer(false)) # Notices 0
19 notice(Float(true)) # Notices 1.0
23 raise(Puppet::ParseError, "bool2num(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
25 value = function_str2bool([arguments[0]])
27 # We have real boolean values as well ...
28 result = value ? 1 : 0
34 # vim: set ts=2 sw=2 et :