Skip to content

Instantly share code, notes, and snippets.

@Kameshwaran
Last active August 29, 2015 14:14
Show Gist options
  • Save Kameshwaran/ad3751971530dae69416 to your computer and use it in GitHub Desktop.
Save Kameshwaran/ad3751971530dae69416 to your computer and use it in GitHub Desktop.
A simple string matcher in Ruby
class String
def method_missing(method, *args, &block)
method_name = method.to_s
if method_name.include?("is_") && method_name.include?("?")
method_name = method_name.gsub("is_", "")[0..-2]
(method_name == self) || (method_name.camelize == self) || (method_name.titleize == self) || (method_name.gsub(" ", "") == self)
else
super(method, *args, &block)
end
end
end
# "kamesh waran".is_kamesh_waran? # returns true
# "kamesh_waran".is_kamesh_waran? # returns true
# "Kamesh Waran".is_kamesh_waran? # returns true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment