Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created May 14, 2011 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquaGeek/971817 to your computer and use it in GitHub Desktop.
Save AquaGeek/971817 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #6588
From 992789d477500b86b38d760d06c0511fd15706f5 Mon Sep 17 00:00:00 2001
From: Ryan Orr <ryanorr12@gmail.com>
Date: Sat, 19 Mar 2011 13:15:21 -0500
Subject: [PATCH] Checks for Enumerable content uniqueness
---
.../lib/active_support/core_ext/enumerable.rb | 11 +++++++++++
activesupport/test/core_ext/enumerable_test.rb | 10 ++++++++++
2 files changed, 21 insertions(+), 0 deletions(-)
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 6d7f771..937ada1 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -100,6 +100,17 @@ module Enumerable
size > 1
end
+ # Returns true if the collection has no duplicated content.
+ # Can be called with a block too, much like any?, so people.uniq? { |p| p.age > 26 } returns true if only 1 person is over 26.
+ def uniq?(&block)
+ if block_given?
+ collection = select(&block)
+ collection.uniq.size == collection.size
+ else
+ uniq.size == size
+ end
+ end
+
# The negative of the Enumerable#include?. Returns true if the collection does not include the object.
def exclude?(object)
!include?(object)
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index 4655bfe..d8aa67e 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -90,6 +90,16 @@ class EnumerableTests < Test::Unit::TestCase
assert [ 1, 2, 2 ].many? {|x| x > 1 }
end
+ def test_uniq
+ assert [].uniq?
+ assert [ 1 ].uniq?
+ assert [ 1, 2 ].uniq?
+ assert ![ 1, 1 ].uniq?
+
+ assert [ 1, 1, 2 ].uniq? { |x| x > 1 }
+ assert ![ 1, 2, 2].uniq? { |x| x > 1 }
+ end
+
def test_exclude?
assert [ 1 ].exclude?(2)
assert ![ 1 ].exclude?(1)
--
1.7.4.1
From d37865248e925629f4b2e835ce5b7b86309aa6de Mon Sep 17 00:00:00 2001
From: Nicolas Cavigneaux <nico@bounga.org>
Date: Wed, 16 Mar 2011 11:59:48 +0100
Subject: [PATCH] Add Enumerable#uniq? to check if an Enumerable has no duplicated content
---
.../lib/active_support/core_ext/enumerable.rb | 10 ++++++++++
activesupport/test/core_ext/enumerable_test.rb | 12 ++++++++++++
2 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 6d7f771..6d4dd56 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -99,6 +99,16 @@ module Enumerable
size = block_given? ? select(&block).size : self.size
size > 1
end
+
+ # Returns true if the collection has no duplicated content.
+ # Can be called with a block too, much like any?, so people.uniq? { |p| p.age > 26 } returns true if only 1 person is over 26.
+ def uniq?(&block)
+ if block_given?
+ select(&block).uniq.size == select(&block).size
+ else
+ uniq.size == size
+ end
+ end
# The negative of the Enumerable#include?. Returns true if the collection does not include the object.
def exclude?(object)
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index 4655bfe..f310b3b 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -90,6 +90,18 @@ class EnumerableTests < Test::Unit::TestCase
assert [ 1, 2, 2 ].many? {|x| x > 1 }
end
+ def test_uniq
+ assert [].uniq?
+ assert [ 1 ].uniq?
+ assert [ 1, 2 ].uniq?
+ assert ![ 1, 1 ].uniq?
+
+ assert [].uniq? { |x| x > 1 }
+ assert [ 2 ].uniq? { |x| x > 1 }
+ assert [ 1, 2 ].uniq? { |x| x > 1 }
+ assert ![ 1, 2, 2 ].uniq? { |x| x > 1 }
+ end
+
def test_exclude?
assert [ 1 ].exclude?(2)
assert ![ 1 ].exclude?(1)
--
1.7.3.2
From d37865248e925629f4b2e835ce5b7b86309aa6de Mon Sep 17 00:00:00 2001
From: Nicolas Cavigneaux <nico@bounga.org>
Date: Wed, 16 Mar 2011 11:59:48 +0100
Subject: [PATCH 1/2] Add Enumerable#uniq? to check if an Enumerable has no duplicated content
---
.../lib/active_support/core_ext/enumerable.rb | 10 ++++++++++
activesupport/test/core_ext/enumerable_test.rb | 12 ++++++++++++
2 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 6d7f771..6d4dd56 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -99,6 +99,16 @@ module Enumerable
size = block_given? ? select(&block).size : self.size
size > 1
end
+
+ # Returns true if the collection has no duplicated content.
+ # Can be called with a block too, much like any?, so people.uniq? { |p| p.age > 26 } returns true if only 1 person is over 26.
+ def uniq?(&block)
+ if block_given?
+ select(&block).uniq.size == select(&block).size
+ else
+ uniq.size == size
+ end
+ end
# The negative of the Enumerable#include?. Returns true if the collection does not include the object.
def exclude?(object)
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index 4655bfe..f310b3b 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -90,6 +90,18 @@ class EnumerableTests < Test::Unit::TestCase
assert [ 1, 2, 2 ].many? {|x| x > 1 }
end
+ def test_uniq
+ assert [].uniq?
+ assert [ 1 ].uniq?
+ assert [ 1, 2 ].uniq?
+ assert ![ 1, 1 ].uniq?
+
+ assert [].uniq? { |x| x > 1 }
+ assert [ 2 ].uniq? { |x| x > 1 }
+ assert [ 1, 2 ].uniq? { |x| x > 1 }
+ assert ![ 1, 2, 2 ].uniq? { |x| x > 1 }
+ end
+
def test_exclude?
assert [ 1 ].exclude?(2)
assert ![ 1 ].exclude?(1)
--
1.7.3.2
From e163fe5f8aff990b6c9c9ed9227dae957795e9d5 Mon Sep 17 00:00:00 2001
From: Nicolas Cavigneaux <nico@bounga.org>
Date: Wed, 16 Mar 2011 13:15:30 +0100
Subject: [PATCH 2/2] Add calculation cache to prevent running it multiple times
---
.../lib/active_support/core_ext/enumerable.rb | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 6d4dd56..a60fe82 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -104,7 +104,8 @@ module Enumerable
# Can be called with a block too, much like any?, so people.uniq? { |p| p.age > 26 } returns true if only 1 person is over 26.
def uniq?(&block)
if block_given?
- select(&block).uniq.size == select(&block).size
+ collection = select(&block)
+ collection.uniq.size == collection.size
else
uniq.size == size
end
--
1.7.3.2
From d37865248e925629f4b2e835ce5b7b86309aa6de Mon Sep 17 00:00:00 2001
From: Nicolas Cavigneaux <nico@bounga.org>
Date: Wed, 16 Mar 2011 11:59:48 +0100
Subject: [PATCH 1/3] Add Enumerable#uniq? to check if an Enumerable has no duplicated content
---
.../lib/active_support/core_ext/enumerable.rb | 10 ++++++++++
activesupport/test/core_ext/enumerable_test.rb | 12 ++++++++++++
2 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 6d7f771..6d4dd56 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -99,6 +99,16 @@ module Enumerable
size = block_given? ? select(&block).size : self.size
size > 1
end
+
+ # Returns true if the collection has no duplicated content.
+ # Can be called with a block too, much like any?, so people.uniq? { |p| p.age > 26 } returns true if only 1 person is over 26.
+ def uniq?(&block)
+ if block_given?
+ select(&block).uniq.size == select(&block).size
+ else
+ uniq.size == size
+ end
+ end
# The negative of the Enumerable#include?. Returns true if the collection does not include the object.
def exclude?(object)
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index 4655bfe..f310b3b 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -90,6 +90,18 @@ class EnumerableTests < Test::Unit::TestCase
assert [ 1, 2, 2 ].many? {|x| x > 1 }
end
+ def test_uniq
+ assert [].uniq?
+ assert [ 1 ].uniq?
+ assert [ 1, 2 ].uniq?
+ assert ![ 1, 1 ].uniq?
+
+ assert [].uniq? { |x| x > 1 }
+ assert [ 2 ].uniq? { |x| x > 1 }
+ assert [ 1, 2 ].uniq? { |x| x > 1 }
+ assert ![ 1, 2, 2 ].uniq? { |x| x > 1 }
+ end
+
def test_exclude?
assert [ 1 ].exclude?(2)
assert ![ 1 ].exclude?(1)
--
1.7.3.2
From e163fe5f8aff990b6c9c9ed9227dae957795e9d5 Mon Sep 17 00:00:00 2001
From: Nicolas Cavigneaux <nico@bounga.org>
Date: Wed, 16 Mar 2011 13:15:30 +0100
Subject: [PATCH 2/3] Add calculation cache to prevent running it multiple times
---
.../lib/active_support/core_ext/enumerable.rb | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 6d4dd56..a60fe82 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -104,7 +104,8 @@ module Enumerable
# Can be called with a block too, much like any?, so people.uniq? { |p| p.age > 26 } returns true if only 1 person is over 26.
def uniq?(&block)
if block_given?
- select(&block).uniq.size == select(&block).size
+ collection = select(&block)
+ collection.uniq.size == collection.size
else
uniq.size == size
end
--
1.7.3.2
From c05454b3418c16825cfd4960270170b24862c69c Mon Sep 17 00:00:00 2001
From: Nicolas Cavigneaux <nico@bounga.org>
Date: Wed, 16 Mar 2011 16:12:55 +0100
Subject: [PATCH 3/3] Better test case for blocks
---
activesupport/test/core_ext/enumerable_test.rb | 5 +----
1 files changed, 1 insertions(+), 4 deletions(-)
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index f310b3b..e5598f6 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -96,10 +96,7 @@ class EnumerableTests < Test::Unit::TestCase
assert [ 1, 2 ].uniq?
assert ![ 1, 1 ].uniq?
- assert [].uniq? { |x| x > 1 }
- assert [ 2 ].uniq? { |x| x > 1 }
- assert [ 1, 2 ].uniq? { |x| x > 1 }
- assert ![ 1, 2, 2 ].uniq? { |x| x > 1 }
+ assert [ 1, 1, 2 ].uniq? { |x| x > 1 }
end
def test_exclude?
--
1.7.3.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment