diff options
author | Eric Putnam <putnam.eric@gmail.com> | 2017-07-27 09:24:52 -0700 |
---|---|---|
committer | Eric Putnam <putnam.eric@gmail.com> | 2017-07-27 09:38:41 -0700 |
commit | 772a2d2f406b65ef65161a04455865b37a1c8456 (patch) | |
tree | 1a8211a374c15fb6fbe31809cf07c1df321315d2 /lib/puppet | |
parent | d65a144aea0cd73f4aaa0cef15c74414baba5c8d (diff) |
(maint) move/rewrite round() as ruby function
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/parser/functions/round.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/round.rb b/lib/puppet/parser/functions/round.rb new file mode 100644 index 0000000..489c301 --- /dev/null +++ b/lib/puppet/parser/functions/round.rb @@ -0,0 +1,33 @@ +# +# round.rb +# + +module Puppet::Parser::Functions + newfunction(:round, :type => :rvalue, :doc => <<-EOS + Rounds a number to the nearest integer + + *Examples:* + + round(2.9) + + returns: 3 + + round(2.4) + + returns: 2 + + EOS + ) do |args| + + raise Puppet::ParseError, "round(): Wrong number of arguments given #{args.size} for 1" if args.size != 1 + raise Puppet::ParseError, "round(): Expected a Numeric, got #{args[0].class}" unless args[0].is_a? Numeric + + value = args[0] + + if value >= 0 + Integer(value + 0.5) + else + Integer(value - 0.5) + end + end +end |