4 module Puppet::Parser::Functions
5 newfunction(:any2bool, :type => :rvalue, :doc => <<-DOC
7 Converts 'anything' to a boolean.
9 In practise it does the following:
10 * Strings such as Y,y,1,T,t,TRUE,yes,'true' will return true
11 * Strings such as 0,F,f,N,n,FALSE,no,'false' will return false
12 * Booleans will just return their original value
13 * Number (or a string representation of a number) > 0 will return true, otherwise false
14 * undef will return false
15 * Anything else will return true
17 Also see the built-in [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
20 @return [Boolean] The boolean value of the object that was given
24 raise(Puppet::ParseError, "any2bool(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
26 # If argument is already Boolean, return it
27 if !!arguments[0] == arguments[0] # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean
42 !!Float(arg) # rubocop:disable Style/DoubleNegation : Could not find a better way to check if a boolean
48 return function_num2bool([arguments[0]])
52 return function_num2bool([arguments[0]]) if valid_float
53 return function_str2bool([arguments[0]])
60 # vim: set ts=2 sw=2 et :