Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10394 from BMorearty/remove-varargs-from-in
Remove varargs from `Object#in?`
- Loading branch information
Showing
3 changed files
with
10 additions
and
31 deletions.
There are no files selected for viewing
28 changes: 9 additions & 19 deletions
28
activesupport/lib/active_support/core_ext/object/inclusion.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,15 @@ | ||
class Object | ||
# Returns true if this object is included in the argument(s). Argument must be | ||
# any object which responds to +#include?+ or optionally, multiple arguments can be passed in. Usage: | ||
# Returns true if this object is included in the argument. Argument must be | ||
# any object which responds to +#include?+. Usage: | ||
# | ||
# characters = ['Konata', 'Kagami', 'Tsukasa'] | ||
# 'Konata'.in?(characters) # => true | ||
# characters = ["Konata", "Kagami", "Tsukasa"] | ||
# "Konata".in?(characters) # => true | ||
# | ||
# character = 'Konata' | ||
# character.in?('Konata', 'Kagami', 'Tsukasa') # => true | ||
# | ||
# This will throw an ArgumentError if a single argument is passed in and it doesn't respond | ||
# This will throw an ArgumentError if the argument doesn't respond | ||
# to +#include?+. | ||
def in?(*args) | ||
if args.length > 1 | ||
args.include? self | ||
else | ||
another_object = args.first | ||
if another_object.respond_to? :include? | ||
another_object.include? self | ||
else | ||
raise ArgumentError.new 'The single parameter passed to #in? must respond to #include?' | ||
end | ||
end | ||
def in?(another_object) | ||
another_object.include?(self) | ||
rescue NoMethodError | ||
raise ArgumentError.new("The parameter passed to #in? must respond to #include?") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters