Skip to content

Instantly share code, notes, and snippets.

@JonathonMA
Created May 25, 2017 02:05
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 JonathonMA/99bd034bfa3d7172400826d5cdf57ec7 to your computer and use it in GitHub Desktop.
Save JonathonMA/99bd034bfa3d7172400826d5cdf57ec7 to your computer and use it in GitHub Desktop.
a bad idea
class Foo
def bar
puts "bar"
end
end
require "did_you_mean"
class LuckyProxy < SimpleDelegator
ReallyNoMethodError = Class.new(StandardError)
def method_missing(meth, *args)
return super
rescue NoMethodError => e
corrections = DidYouMean::MethodNameChecker.new(e).corrections
if corrections.size == 1
puts "You got lucky, assuming you did mean: #{corrections.first}"
send(corrections.first, *args)
else
raise ReallyNoMethodError, e.message
end
end
end
class Object
def ‽
LuckyProxy.new(self)
end
end
puts "Unlucky:"
begin
Foo.new.baz
rescue => e
puts e
end
puts "Lucky:"
Foo.new.‽.baz
@JonathonMA
Copy link
Author

Unlucky:
undefined method `baz' for #<Foo:0x007fba9300a780>
Did you mean?  bar
Lucky:
You got lucky, assuming you did mean: bar
bar

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