Skip to content

Instantly share code, notes, and snippets.

@ckazu
Created February 20, 2012 17:47
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 ckazu/1870273 to your computer and use it in GitHub Desktop.
Save ckazu/1870273 to your computer and use it in GitHub Desktop.
require 'typo_fixer'
class Object
include TypoFixer
end
p [1, 2, 3].shaflfe.joni #=> "213"
require 'typo_fixer'
class Sample
include TypoFixer
def some_method
'some value'
end
end
sample = Sample.new
p sample.sme_motoehd #=> "some value"
module TypoFixer
def self.included(base)
base.class_eval do
alias_method :method_missing_without_fix_typo, :method_missing
alias_method :method_missing, :method_missing_with_fix_typo
end
end
def method_missing_with_fix_typo(name, *args)
if name && method = find_method(name)
$stderr.puts "WARNING: execute `#{name}` as `#{method}` for #{self}"
send(method, *args)
else
method_missing_without_fix_typo(name, *args)
end
end
private
def find_method name
name.to_s.split(//).permutation.each do |candidate|
_candidate = candidate.join
return _candidate if respond_to?(_candidate, true)
end
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment