Created
February 22, 2012 07:56
-
-
Save ckazu/1883331 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'typo_fixer' | |
| class Object | |
| include TypoFixer | |
| end | |
| p "some string".revarse #=> "gnirts emos" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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