Skip to content

Commit 0c86974

Browse files
authored
Merge pull request #798 from eputnam/round_func
(maint) move/rewrite round() as ruby function
2 parents a8ce26d + 772a2d2 commit 0c86974

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ For example, `reject(['aaa','bbb','ccc','aaaddd'], 'aaa')` returns ['bbb','ccc']
15501550
15511551
Reverses the order of a string or array.
15521552
1553-
#### `stdlib::round`
1553+
#### `round`
15541554
15551555
Rounds a number to the nearest integer
15561556

functions/round.pp

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/puppet/parser/functions/round.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# round.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:round, :type => :rvalue, :doc => <<-EOS
7+
Rounds a number to the nearest integer
8+
9+
*Examples:*
10+
11+
round(2.9)
12+
13+
returns: 3
14+
15+
round(2.4)
16+
17+
returns: 2
18+
19+
EOS
20+
) do |args|
21+
22+
raise Puppet::ParseError, "round(): Wrong number of arguments given #{args.size} for 1" if args.size != 1
23+
raise Puppet::ParseError, "round(): Expected a Numeric, got #{args[0].class}" unless args[0].is_a? Numeric
24+
25+
value = args[0]
26+
27+
if value >= 0
28+
Integer(value + 0.5)
29+
else
30+
Integer(value - 0.5)
31+
end
32+
end
33+
end

spec/functions/round_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
require 'spec_helper'
22

3-
describe 'stdlib::round' do
3+
describe 'round' do
44
it { is_expected.not_to eq(nil) }
55
it { is_expected.to run.with_params(34.3).and_return(34) }
66
it { is_expected.to run.with_params(-34.3).and_return(-34) }
77
it { is_expected.to run.with_params(34.5).and_return(35) }
88
it { is_expected.to run.with_params(-34.5).and_return(-35) }
99
it { is_expected.to run.with_params(34.7).and_return(35) }
1010
it { is_expected.to run.with_params(-34.7).and_return(-35) }
11+
it { is_expected.to run.with_params("test").and_raise_error Puppet::ParseError }
12+
it { is_expected.to run.with_params("test", "best").and_raise_error Puppet::ParseError }
13+
it { is_expected.to run.with_params(3, 4).and_raise_error Puppet::ParseError }
1114
end

0 commit comments

Comments
 (0)