Skip to content

Instantly share code, notes, and snippets.

@dallas dallas/string.rb
Created Feb 16, 2010

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.