11. 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
https://gist.github.com/1870273
12. require 'typo_fixer'
class Sample
include TypoFixer
def some_method
'some value'
end
end
sample = Sample.new
p sample.sme_motoehd #=> 'some value'
p sample.sme_motoehd.reveres #=> error!
https://gist.github.com/1870273
15. 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 method = find_method(name)
$stdout.print "WARNING: execute `#{name}` as `#{method}`? [Y/n] "
return send(method, *args) if ($stdin.getc == 'Y')
end
method_missing_without_fix_typo(name, *args)
end
private
def find_method name
self.class.instance_methods.each do |method|
next unless method.size == name.size
if(method.to_s.split(//) - name.to_s.split(//)).size == 1
return method
end
end
end
end https://gist.github.com/1883331