Skip to content

Commit b6830f1

Browse files
committed
Merge pull request #365 from dalen/range-integers
Make the range function work with integers
2 parents b80c432 + af0a277 commit b6830f1

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

lib/puppet/parser/functions/range.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ module Puppet::Parser::Functions
6565
end
6666
end
6767

68-
# Check whether we have integer value if so then make it so ...
69-
if start.match(/^\d+$/)
70-
start = start.to_i
71-
stop = stop.to_i
72-
else
73-
start = start.to_s
74-
stop = stop.to_s
75-
end
68+
# Check whether we have integer value if so then make it so ...
69+
if start.to_s.match(/^\d+$/)
70+
start = start.to_i
71+
stop = stop.to_i
72+
else
73+
start = start.to_s
74+
stop = stop.to_s
75+
end
7676

77-
range = case type
78-
when /^(\.\.|\-)$/ then (start .. stop)
79-
when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ...
80-
end
77+
range = case type
78+
when /^(\.\.|\-)$/ then (start .. stop)
79+
when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ...
80+
end
8181

82-
result = range.step(step).collect { |i| i } # Get them all ... Pokemon ...
82+
result = range.step(step).collect { |i| i } # Get them all ... Pokemon ...
8383

8484
return result
8585
end

spec/functions/range_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,20 @@
6767
expect(scope.function_range(["00", "10"])).to eq expected
6868
end
6969
end
70+
71+
describe 'with a numeric range' do
72+
it "returns a range of numbers" do
73+
expected = (1..10).to_a
74+
expect(scope.function_range([1,10])).to eq expected
75+
end
76+
it "returns a range of numbers with step parameter" do
77+
expected = (1..10).step(2).to_a
78+
expect(scope.function_range([1,10,2])).to eq expected
79+
end
80+
it "works with mixed numeric like strings and numeric arguments" do
81+
expected = (1..10).to_a
82+
expect(scope.function_range(['1',10])).to eq expected
83+
expect(scope.function_range([1,'10'])).to eq expected
84+
end
85+
end
7086
end

0 commit comments

Comments
 (0)