Created
February 20, 2012 17:47
-
-
Save ckazu/1870273 to your computer and use it in GitHub Desktop.
This file contains 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 [1, 2, 3].shaflfe.joni #=> "213" |
This file contains 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 Sample | |
include TypoFixer | |
def some_method | |
'some value' | |
end | |
end | |
sample = Sample.new | |
p sample.sme_motoehd #=> "some value" |
This file contains 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 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