Upgrade to Pro — share decks privately, control downloads, hide ads and more …

typo の傾向と対策

ckazu
February 22, 2012

typo の傾向と対策

ckazu

February 22, 2012
Tweet

More Decks by ckazu

Other Decks in Programming

Transcript

  1. about me •@ckazu • •౦ژ։ൃG •Rails ʹΑΔαΠτ։ൃ •Rails + JS

    ʹΑΔ ϒϥ΢βήʔϜ։ൃ •Rails, Ruby Λۀ຿Ͱ࢖͍࢝Ίͯ17ϲ݄
  2. ղܾࡦ̍ 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 ૊Έ߹ΘͤΛશνΣοΫ
  3. ղܾࡦ̍ 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
  4. ղܾࡦ̍ require 'typo_fixer' class Object include TypoFixer end p [1,

    2, 3].shaflfe.joni #=> "213" https://gist.github.com/1870273 ͜Θ͍Ͱ͢Ͷɽ
  5. ղܾࡦ̎ 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 ͱΓ͋͑ͣͦΕͬΆ͍ͷΛฦ͢ ഑ྻͷํ͕ྑ͍Ͱ͢Ͷ
  6. ղܾࡦ̎ require 'typo_fixer' class Object include TypoFixer end p "some

    string".revarse #=> "gnirts emos" https://gist.github.com/1870273