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