aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/object/inclusion.rb
diff options
context:
space:
mode:
authorPrem Sichanugrist, Brian Morearty, John Reitano <s@sikachu.com, github2@morearty.org, john_reitano@yahoo.com>2011-04-10 20:27:08 +0800
committerDavid Heinemeier Hansson <david@loudthinking.com>2011-04-11 03:17:08 +0800
commit635d991683c439da56fa72853880e88e6ac291ed (patch)
treecb2d51ebe3bedfb71df5661ba5e86246bf064e0a /activesupport/lib/active_support/core_ext/object/inclusion.rb
parent62b2755f7a4ca12491a7899348066695a6a26917 (diff)
downloadrails-635d991683c439da56fa72853880e88e6ac291ed.tar.gz
rails-635d991683c439da56fa72853880e88e6ac291ed.tar.bz2
rails-635d991683c439da56fa72853880e88e6ac291ed.zip
Add support for Object#in? and Object#either? in Active Support [#6321 state:committed]
This will allow you to check if an object is included in another object or the list of objects or not. This patch is derived from patch by Brian Morearty and John Reitano on Lighthouse ticket. I've rewrite it and make sure that we support both 'another object' and 'list of objects' version, as it surely be useful to support both.
Diffstat (limited to 'activesupport/lib/active_support/core_ext/object/inclusion.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/object/inclusion.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/inclusion.rb b/activesupport/lib/active_support/core_ext/object/inclusion.rb
new file mode 100644
index 0000000000..79e9fd6c88
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/object/inclusion.rb
@@ -0,0 +1,20 @@
+class Object
+ # Returns true if this object is included in the argument. Argument must be
+ # any object which respond to +#include?+. Usage:
+ #
+ # characters = ["Konata", "Kagami", "Tsukasa"]
+ # "Konata".in?(characters) # => true
+ #
+ def in?(another_object)
+ another_object.include?(self)
+ end
+
+ # Returns true if this object is included in the argument list. Usage:
+ #
+ # username = "sikachu"
+ # username.either?("josevalim", "dhh", "wycats") # => false
+ #
+ def either?(*objects)
+ objects.include?(self)
+ end
+end