Ruby < 1.8.7 doesn't have this method and we're using it in a test, so
tests won't run on 1.8.6 until this is in place.

It's probably a good thing to use much in implementation since it's
written in pure Ruby when using < 1.8.7 and in C when in > 1.8.7, but
then if you're using older Rubies you're probably not expecting much for
performance anyway.

Reviewed-by: Daniel Pittman <dan...@puppetlabs.com>
Signed-off-by: Matt Robinson <m...@puppetlabs.com>
---
Local-branch: ticket/next/maint-backport_array_combination
 lib/puppet/util/monkey_patches.rb     |   14 ++++++++++++++
 spec/unit/util/monkey_patches_spec.rb |   23 +++++++++++++++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/lib/puppet/util/monkey_patches.rb 
b/lib/puppet/util/monkey_patches.rb
index a93c66b..10a2684 100644
--- a/lib/puppet/util/monkey_patches.rb
+++ b/lib/puppet/util/monkey_patches.rb
@@ -90,3 +90,17 @@ if RUBY_VERSION == '1.8.1' || RUBY_VERSION == '1.8.2'
     r
   }
 end
+
+class Array
+  # Ruby < 1.8.7 doesn't have this method but we use it in tests
+  def combination(num)
+    return [] if num < 0 || num > size
+    return [[]] if num == 0
+    return map{|e| [e] } if num == 1
+    tmp = self.dup
+    self[0, size - (num - 1)].inject([]) do |ret, e|
+      tmp.shift
+      ret += tmp.combination(num - 1).map{|a| a.unshift(e) }
+    end
+  end unless method_defined? :combination
+end
diff --git a/spec/unit/util/monkey_patches_spec.rb 
b/spec/unit/util/monkey_patches_spec.rb
index 8bfcd90..fb2103e 100644
--- a/spec/unit/util/monkey_patches_spec.rb
+++ b/spec/unit/util/monkey_patches_spec.rb
@@ -31,3 +31,26 @@ describe "yaml deserialization" do
     obj.foo.should == 100
   end
 end
+
+# In Ruby > 1.8.7 this is a builtin, otherwise we monkey patch the method in
+describe "Array#combination" do
+  it "should fail if wrong number of arguments given" do
+    lambda { [1,2,3].combination() }.should raise_error(ArgumentError, /wrong 
number/)
+    lambda { [1,2,3].combination(1,2) }.should raise_error(ArgumentError, 
/wrong number/)
+  end
+
+  it "should return an empty array if combo size than array size or negative" 
do
+    [1,2,3].combination(4).to_a.should == []
+    [1,2,3].combination(-1).to_a.should == []
+  end
+
+  it "should return an empty array with an empty array if combo size == 0" do
+    [1,2,3].combination(0).to_a.should == [[]]
+  end
+
+  it "should all provide all combinations of size passed in" do
+    [1,2,3,4].combination(1).to_a.should == [[1], [2], [3], [4]]
+    [1,2,3,4].combination(2).to_a.should == [[1, 2], [1, 3], [1, 4], [2, 3], 
[2, 4], [3, 4]]
+    [1,2,3,4].combination(3).to_a.should == [[1, 2, 3], [1, 2, 4], [1, 3, 4], 
[2, 3, 4]]
+  end
+end
-- 
1.7.3.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to puppet-dev@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to