Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit 635d991683c439da56fa72853880e88e6ac291ed 1 parent 62b2755
Prem Sichanugrist, Brian Morearty, John Reitano authored dhh committed
View
2  activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Rails 3.1.0 (unreleased)*
+* Add Object#in? to test if an object is included in another object, and Object#either? to test if an object is included in a list of objects which will be passed as arguments. [Prem Sichanugrist, Brian Morearty, John Reitano]
+
* LocalCache strategy is now a real middleware class, not an anonymous class
posing for pictures.
View
20 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"]
@chuyeow
chuyeow added a note

Love the reference to Lucky Star :)

@pigoz
pigoz added a note

Moe got his way into Rails :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ # "Konata".in?(characters) # => true
+ #
+ def in?(another_object)
+ another_object.include?(self)
+ end
@tenderlove Owner

What should the behavior of this code be:

"foo".in?(nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ # 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
View
52 activesupport/test/core_ext/object/inclusion_test.rb
@@ -0,0 +1,52 @@
+require 'abstract_unit'
+require 'active_support/core_ext/object/inclusion'
+
+class InTest < Test::Unit::TestCase
+ def test_in_array
+ assert 1.in?([1,2])
+ assert !3.in?([1,2])
+ end
+
+ def test_in_hash
+ h = { "a" => 100, "b" => 200 }
+ assert "a".in?(h)
+ assert !"z".in?(h)
+ end
+
+ def test_in_string
+ assert "lo".in?("hello")
+ assert !"ol".in?("hello")
+ assert ?h.in?("hello")
+ end
+
+ def test_in_range
+ assert 25.in?(1..50)
+ assert !75.in?(1..50)
+ end
+
+ def test_in_set
+ s = Set.new([1,2])
+ assert 1.in?(s)
+ assert !3.in?(s)
+ end
+
+ def test_either
+ assert 1.either?(1,2,3)
+ assert !5.either?(1,2,3)
+ assert [1,2,3].either?([1,2,3], 2, [3,4,5])
+ end
+
+ module A
+ end
+ class B
+ include A
+ end
+ class C < B
+ end
+
+ def test_in_module
+ assert A.in?(B)
+ assert A.in?(C)
+ assert !A.in?(A)
+ end
+end

3 comments on commit 635d991

@fxn
Owner

Would be good to update also the AS guide.

@tenderlove

What should the behavior of this code be:

"foo".in?(nil)
@amatsuda
Collaborator

Nice :)
But I prefer extlib version. https://github.com/datamapper/extlib/blob/master/lib/extlib/object.rb#L163

I just would like to use the shorter method name in any case.

@smartinez87

Updated AS guide to include this new methods: rails/docrails@7a1189a

Please sign in to comment.
Something went wrong with that request. Please try again.