Skip to content

Instantly share code, notes, and snippets.

@groyoh
Created August 21, 2015 11:38
Show Gist options
  • Save groyoh/79784565fe652668c708 to your computer and use it in GitHub Desktop.
Save groyoh/79784565fe652668c708 to your computer and use it in GitHub Desktop.
Method missing suggestions
require "bundler/inline"
gemfile(true) do
gem "fuzzy-string-match", require: "fuzzystringmatch"
gem "pry"
end
class Bar
def method_missing(m)
raise "ahaha"
end
end
class Object
JAROW = ::FuzzyStringMatch::JaroWinkler.create( :native )
def method_missing(method, *args, &block)
method = method.to_s
matched_method = methods.max_by do |m|
JAROW.getDistance(m.to_s, method)
end
raise ::NoMethodError, "did you meant #{matched_method}?"
end
end
class Foo
def foo; end
def bar; end
def foo_bar; end
def very_complicated_method_name; end
end
f = Foo.new
begin
f.fooo
rescue => e
puts e
end
begin
f.fooobar
rescue => e
puts e
end
begin
f.very_complcicated_method_name
rescue => e
puts e
end
Bar.new.foo
__END__
did you meant foo?
did you meant foo_bar?
did you meant very_complicated_method_name?
fuzzy_method_missing.rb:11:in `method_missing': ahaha (RuntimeError)
from fuzzy_method_missing.rb:62:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment