typo の傾向と対策

2,095 views
2,001 views

Published on

shinjuku.rb #02 LT

1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
2,095
On SlideShare
0
From Embeds
0
Number of Embeds
85
Actions
Shares
0
Downloads
3
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

typo の傾向と対策

  1. 1. typo 2012-02-22 kchinda@aiming-inc.com (a.k.a. @ckazu)
  2. 2. about me• @ckazu• • G • Rails • Rails + JS• Rails, Ruby 17
  3. 3. Shinjuku.rb #01
  4. 4. http://www.slideshare.net/ckazu/rails5
  5. 5. typo
  6. 6. typo
  7. 7. • typo
  8. 8. typo
  9. 9. • • User.count → User.coutn
  10. 10. 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  endend https://gist.github.com/1870273
  11. 11. require typo_fixerclass Sample  include TypoFixer  def some_method    some value  endendsample = Sample.newp sample.sme_motoehd #=> some valuep sample.sme_motoehd.reveres #=> error! https://gist.github.com/1870273
  12. 12. require typo_fixerclass Object  include TypoFixerendp [1, 2, 3].shaflfe.joni #=> "213" https://gist.github.com/1870273
  13. 13. • • User.count → User.caunt
  14. 14. 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  endend https://gist.github.com/1883331
  15. 15. require typo_fixerclass Object  include TypoFixerendp "some string".revarse #=> "gnirts emos" https://gist.github.com/1870273
  16. 16. • • array.flatten → array.flaten
  17. 17.
  18. 18. • • car.control → car.controll
  19. 19.
  20. 20. • • User.fin_dvy_id(di)
  21. 21.
  22. 22. • method typo
  23. 23.
  24. 24. http://suburi.herokuapp.com/

×