Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / max.rb
1 module Puppet::Parser::Functions
2   newfunction(:max, :type => :rvalue, :doc => <<-EOS
3     Returns the highest value of all arguments.
4     Requires at least one argument.
5     EOS
6   ) do |args|
7
8     raise(Puppet::ParseError, "max(): Wrong number of arguments need at least one") if args.size == 0
9
10     # Sometimes we get numbers as numerics and sometimes as strings.
11     # We try to compare them as numbers when possible
12     return args.max do |a,b|
13       if a.to_s =~ /\A-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then
14         a.to_f <=> b.to_f
15       else
16         a.to_s <=> b.to_s
17       end
18     end
19   end
20 end