Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 26 lines (25 sloc) 0.83 kB
635d991 Add support for Object#in? and Object#either? in Active Support [#632…
Prem Sichanugrist, Brian Morearty, John Reitano authored
1 class Object
ebf69ab Object#in? also accepts multiple parameters
Tadas Tamošauskas authored
2 # Returns true if this object is included in the argument(s). Argument must be
3 # any object which responds to +#include?+ or optionally, multiple arguments can be passed in. Usage:
635d991 Add support for Object#in? and Object#either? in Active Support [#632…
Prem Sichanugrist, Brian Morearty, John Reitano authored
4 #
432a65f @gazay String quotes and trailing spaces
gazay authored
5 # characters = ['Konata', 'Kagami', 'Tsukasa']
6 # 'Konata'.in?(characters) # => true
1946d7b @gazay AS core_ext refactoring
gazay authored
7 #
432a65f @gazay String quotes and trailing spaces
gazay authored
8 # character = 'Konata'
9 # character.in?('Konata', 'Kagami', 'Tsukasa') # => true
635d991 Add support for Object#in? and Object#either? in Active Support [#632…
Prem Sichanugrist, Brian Morearty, John Reitano authored
10 #
ebf69ab Object#in? also accepts multiple parameters
Tadas Tamošauskas authored
11 # This will throw an ArgumentError if a single argument is passed in and it doesn't respond
cd233dd @jaredonline Only rescue a thrown NoMethodError, don't preemptively check for #inc…
jaredonline authored
12 # to +#include?+.
ebf69ab Object#in? also accepts multiple parameters
Tadas Tamošauskas authored
13 def in?(*args)
14 if args.length > 1
15 args.include? self
16 else
17 another_object = args.first
18 if another_object.respond_to? :include?
19 another_object.include? self
20 else
1946d7b @gazay AS core_ext refactoring
gazay authored
21 raise ArgumentError.new 'The single parameter passed to #in? must respond to #include?'
ebf69ab Object#in? also accepts multiple parameters
Tadas Tamošauskas authored
22 end
23 end
635d991 Add support for Object#in? and Object#either? in Active Support [#632…
Prem Sichanugrist, Brian Morearty, John Reitano authored
24 end
25 end
Something went wrong with that request. Please try again.