Skip to content

Instantly share code, notes, and snippets.

@ckazu
Created February 22, 2012 07:56
Show Gist options
  • Select an option

  • Save ckazu/1883331 to your computer and use it in GitHub Desktop.

Select an option

Save ckazu/1883331 to your computer and use it in GitHub Desktop.
require 'typo_fixer'
class Object
include TypoFixer
end
p "some string".revarse #=> "gnirts emos"
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
return method if(method.to_s.split(//) - name.to_s.split(//)).size == 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment