Skip to content

Instantly share code, notes, and snippets.

@dallas
Created February 16, 2010 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dallas/305807 to your computer and use it in GitHub Desktop.
Save dallas/305807 to your computer and use it in GitHub Desktop.
add single-word boolean methods to String
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
@dallas
Copy link
Author

dallas commented Jan 24, 2011

Already a part of Rails in the form of ActiveSupport::StringInquirer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment