View
151
Download
8
Category
Preview:
DESCRIPTION
shinjuku.rb #02 LT
Citation preview
typo の傾向と対策2012-02-22
kchinda@aiming-inc.com(a.k.a. @ckazu)
about me
•@ckazu•
•東京開発G•Rails によるサイト開発•Rails + JS によるブラウザゲーム開発
•Rails, Ruby を業務で使い始めて17ヶ月
Shinjuku.rb #01
http://www.slideshare.net/ckazu/rails5
みなさん
typo してますか
typo をなくしましょう
今日の内容
•typo いかに克服するか
typo の傾向と対策
傾向その1
•入れ替わり•User.count → User.coutn
解決策1module 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 endend https://gist.github.com/1870273
組み合わせを全チェック
解決策1require 'typo_fixer'
class Sample include TypoFixer
def some_method 'some value' endend
sample = Sample.newp sample.sme_motoehd #=> 'some value'p sample.sme_motoehd.reveres #=> error!
https://gist.github.com/1870273
解決策1require 'typo_fixer'
class Object include TypoFixerend
p [1, 2, 3].shaflfe.joni #=> "213"
https://gist.github.com/1870273
こわいですね.
傾向その2
•一文字だけ違う•User.count → User.caunt
解決策2module 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 endend https://gist.github.com/1883331
とりあえずそれっぽいのを返す
配列の方が良いですね
解決策2require 'typo_fixer'
class Object include TypoFixerend
p "some string".revarse #=> "gnirts emos"
https://gist.github.com/1870273
傾向その3
•少ない•array.flatten → array.flaten
解決策3
•配列にして比較したときにひとつだけ違うので,解決策2とほぼ同じ
傾向その4
•多い•car.control → car.controll
解決策4
•配列にして比較したときにひとつだけ違うので,解決策2とほぼ同じ
傾向その5
•複合型•User.fin_dvy_id(di)
解決策5
•ここまで気にしていられない
解決策?
•定義されている method と typo との単語間の距離(類似度)を算出して候補を提案する
誰か作ってください
番外編
番外編
•体育会系解決策
素振り
タイピングも準備運動大事
基礎練大事
朝練しましょう
就業時には居残り特訓しましょう
無性にタイピングしまくりたくなることありませんか?
プログラマのための素振りサイト
http://suburi.herokuapp.com/
超プロトタイプ版!
バグてんこ盛り!
終
Recommended