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
•

    • User.count   → User.coutn
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
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
require 'typo_fixer'

class Object
  include TypoFixer
end

p [1, 2, 3].shaflfe.joni #=> "213"




                        https://gist.github.com/1870273
•

    • User.count   → User.caunt
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
require 'typo_fixer'

class Object
  include TypoFixer
end

p "some string".revarse #=> "gnirts emos"




                        https://gist.github.com/1870273
•

    • array.flatten   → array.flaten
•
•

    • car.control   → car.controll
•
•

    • User.fin_dvy_id(di)
•
•   method   typo
•
http://suburi.herokuapp.com/
typo の傾向と対策

typo の傾向と対策

  • 1.
    typo 2012-02-22 kchinda@aiming-inc.com (a.k.a. @ckazu)
  • 2.
    about me • @ckazu • • G • Rails • Rails + JS • Rails, Ruby 17
  • 3.
  • 4.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
    • User.count → User.coutn
  • 11.
    module TypoFixer   def self.included(base)     base.class_evaldo       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
  • 12.
    require 'typo_fixer' class Sample   includeTypoFixer   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
  • 13.
    require 'typo_fixer' class Object   includeTypoFixer end p [1, 2, 3].shaflfe.joni #=> "213" https://gist.github.com/1870273
  • 14.
    • User.count → User.caunt
  • 15.
    module TypoFixer   def self.included(base)     base.class_evaldo       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
  • 16.
    require 'typo_fixer' class Object   includeTypoFixer end p "some string".revarse #=> "gnirts emos" https://gist.github.com/1870273
  • 17.
    • array.flatten → array.flaten
  • 18.
  • 19.
    • car.control → car.controll
  • 20.
  • 21.
    • User.fin_dvy_id(di)
  • 22.
  • 23.
    method typo
  • 26.
  • 33.