From 635d991683c439da56fa72853880e88e6ac291ed Mon Sep 17 00:00:00 2001 From: "Prem Sichanugrist, Brian Morearty, John Reitano" Date: Sun, 10 Apr 2011 20:27:08 +0800 Subject: 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. --- .../lib/active_support/core_ext/object/inclusion.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/object/inclusion.rb (limited to 'activesupport/lib/active_support/core_ext/object/inclusion.rb') 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 -- cgit v1.2.3