diff options
author | Hunter Haugen <hunter@puppetlabs.com> | 2014-11-13 10:16:21 -0800 |
---|---|---|
committer | Hunter Haugen <hunter@puppetlabs.com> | 2014-11-13 10:16:21 -0800 |
commit | b6830f1ade27bf1dad9c75d45ee21cebcddf11f6 (patch) | |
tree | cd9a343bebe0f752b34c835c31dfa91ddabc7bd8 | |
parent | b80c432ab9de2536aab099e5d5b3baf9e883bc68 (diff) | |
parent | af0a2779cb63b09a07f675ede3ae0b959c7442f6 (diff) | |
download | puppet-stdlib-b6830f1ade27bf1dad9c75d45ee21cebcddf11f6.tar.gz puppet-stdlib-b6830f1ade27bf1dad9c75d45ee21cebcddf11f6.tar.bz2 |
Merge pull request #365 from dalen/range-integers
Make the range function work with integers
-rw-r--r-- | lib/puppet/parser/functions/range.rb | 26 | ||||
-rwxr-xr-x | spec/functions/range_spec.rb | 16 |
2 files changed, 29 insertions, 13 deletions
diff --git a/lib/puppet/parser/functions/range.rb b/lib/puppet/parser/functions/range.rb index ffbdf84..49fba21 100644 --- a/lib/puppet/parser/functions/range.rb +++ b/lib/puppet/parser/functions/range.rb @@ -65,21 +65,21 @@ Will return: [0,2,4,6,8] end end - # Check whether we have integer value if so then make it so ... - if start.match(/^\d+$/) - start = start.to_i - stop = stop.to_i - else - start = start.to_s - stop = stop.to_s - end + # Check whether we have integer value if so then make it so ... + if start.to_s.match(/^\d+$/) + start = start.to_i + stop = stop.to_i + else + start = start.to_s + stop = stop.to_s + end - range = case type - when /^(\.\.|\-)$/ then (start .. stop) - when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... - end + range = case type + when /^(\.\.|\-)$/ then (start .. stop) + when /^(\.\.\.)$/ then (start ... stop) # Exclusive of last element ... + end - result = range.step(step).collect { |i| i } # Get them all ... Pokemon ... + result = range.step(step).collect { |i| i } # Get them all ... Pokemon ... return result end diff --git a/spec/functions/range_spec.rb b/spec/functions/range_spec.rb index 9b9ece0..ef86f97 100755 --- a/spec/functions/range_spec.rb +++ b/spec/functions/range_spec.rb @@ -67,4 +67,20 @@ describe "the range function" do expect(scope.function_range(["00", "10"])).to eq expected end end + + describe 'with a numeric range' do + it "returns a range of numbers" do + expected = (1..10).to_a + expect(scope.function_range([1,10])).to eq expected + end + it "returns a range of numbers with step parameter" do + expected = (1..10).step(2).to_a + expect(scope.function_range([1,10,2])).to eq expected + end + it "works with mixed numeric like strings and numeric arguments" do + expected = (1..10).to_a + expect(scope.function_range(['1',10])).to eq expected + expect(scope.function_range([1,'10'])).to eq expected + end + end end |