Created
January 28, 2015 07:59
-
-
Save oakbow/7229d343cf34c02957b1 to your computer and use it in GitHub Desktop.
Object.in?でArgumentErrorが発生した場合の対処(Rails3 => Rails4) ref: http://qiita.com/Oakbow/items/f1d3f2b69fd056488162
This file contains hidden or 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
ArgumentError - wrong number of arguments (2 for 1): |
This file contains hidden or 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
# 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 | |
# | |
# This will throw an ArgumentError if the argument doesn't respond | |
# to +#include?+. | |
def in?(another_object) | |
another_object.include?(self) | |
rescue NoMethodError | |
raise ArgumentError.new("The parameter passed to #in? must respond to #include?") | |
end |
This file contains hidden or 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
object.in?('hogehogehoge', 'fugafugafuga') | |
↓ | |
object.in?(['hogehogehoge', 'fugafugafuga']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment