Redefine #respond_to_missing with #method_missing
Initial attempt:
class User
def method_missing(name, *args, &block)
if "#{name}" =~ /foo/
'exists'
else
super
end
end
end
> u = User.new
=> #<User:0x007fdf3b152c60>
> u.foo
=> exists
> u.method(:foo)
=> NameError: undefined method `foo' for class `User'
User.class_eval do
def respond_to_missing?(name, include_private = false)
"#{name}" =~ /foo/ || super
end
end
> u = User.new
=> #<User:0x007ffcda847f90>
> u.method(:foo)
=> #<Method: User#foo>
Tweet