Created
February 16, 2010 19:13
-
-
Save dallas/305807 to your computer and use it in GitHub Desktop.
add single-word boolean methods to String
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
class String | |
# alias the method_missing method chain so that we can use single-word booleans | |
# for example, we can do things like "standard".standard? which will give us true | |
# obviously this is most suited for cases where you have a variable with a value from a known set of values | |
def method_missing_with_single_word_booleans(method_name, *args, &block) | |
return method_missing_without_single_word_booleans(method_name, *args, &block) unless method_name.to_s =~ /^(\w+)\?$/ | |
self == $1 | |
end | |
alias_method_chain :method_missing, :single_word_booleans | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Already a part of Rails in the form of
ActiveSupport::StringInquirer