summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions/max.rb
diff options
context:
space:
mode:
authorErik Dalén <dalen@spotify.com>2013-03-20 16:36:20 +0100
committerAdrien Thebo <git@somethingsinistral.net>2013-03-27 14:03:31 -0700
commitff5dd5d75adb3723e106ca20bac4e68466395a56 (patch)
tree7e2881f09efa19e7bc84e19ddfcf263e92b1627b /lib/puppet/parser/functions/max.rb
parent4c695ede384501b9c516cc53bdfc6b547a6e5ea2 (diff)
Allow comparisons of Numeric and number as String
Puppet passes numbers as String to functions, but it makes more sense to compare them as Numeric. But sometimes Puppet passes them as the wrong type, see: https://projects.puppetlabs.com/issues/19812
Diffstat (limited to 'lib/puppet/parser/functions/max.rb')
-rw-r--r--lib/puppet/parser/functions/max.rb10
1 files changed, 9 insertions, 1 deletions
diff --git a/lib/puppet/parser/functions/max.rb b/lib/puppet/parser/functions/max.rb
index 10b6f74..60fb94a 100644
--- a/lib/puppet/parser/functions/max.rb
+++ b/lib/puppet/parser/functions/max.rb
@@ -8,6 +8,14 @@ module Puppet::Parser::Functions
raise(Puppet::ParseError, "max(): Wrong number of arguments " +
"need at least one") if args.size == 0
- return args.max
+ # Sometimes we get numbers as numerics and sometimes as strings.
+ # We try to compare them as numbers when possible
+ return args.max do |a,b|
+ if a.to_s =~ /\A-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then
+ a.to_f <=> b.to_f
+ else
+ a.to_s <=> b.to_s
+ end
+ end
end
end