Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example of how to rewrite method_missing and respond_to? in Ruby
class CarPart
# does the method name end in _price ?
def ghost_method_condition(name)
# rewrite method missing if the method matches ghost_method_condition
def method_missing(name, *args, &block)
super unless ghost_method_condition(name)
# do something else...
# rewrite respond_to? to include ghost methods
def respond_to?(name, include_private = false)
ghost_method_condition(name) || super
c =
puts c.respond_to? :stereo_price
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment