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

CHINDOGU: LEVENSHTEIN Y METHOD MISSNG

CHINDOGU: LEVENSHTEIN Y METHOD MISSNG

Ivan Acosta-Rubio

October 02, 2012
Tweet

More Decks by Ivan Acosta-Rubio

Other Decks in Technology

Transcript

  1. ¿Que es un Chindogu? Es un invento que, aparentemente, es

    la solución ideal a un problema particular pero que en la práctica resulta todo lo contrario. Los inconvenientes adicionales suelen hacer que dicho ingenio sea inapropiado o absurdo. Tuesday, October 2, 12
  2. Distancia de Levenshtein el número mínimo de operaciones requeridas para

    transformar una cadena de caracteres en otra. Tuesday, October 2, 12
  3. describe WTF do let(:wtf) { WTF.new } it "does just

    as expected" do wtf.attempt.should == "wtf" end end Tuesday, October 2, 12
  4. describe WTF do let(:wtf) { WTF.new } it "does just

    as expected" do wtf.attempt.should == "wtf" end it "calls a method with distance 1" do wtf.attemp.should == "wtf" end it "calls another method with distance 1" do wtf.atempt.should == "wtf" end end Tuesday, October 2, 12
  5. method_missing Permite interceptar mensajes que no se puede “responder” y

    hacer con ellos lo que queramos. Te permite interceptar NoMethodError y hacer lo que quieras desde alli. Tuesday, October 2, 12
  6. require 'levenshtein' module ICANHAZTYPOS def method_missing(name, *args, &block) methods.each do

    |m| return self.send(m) if Levenshtein.distance(m.to_s, name.to_s) == 1 end end end Tuesday, October 2, 12
  7. class WTF include ICANHAZTYPOS def attempt "wtf" end end ruby-1.9.3-p0

    :001 > w = WTF.new => #<WTF:0x007ff7e0c74428> ruby-1.9.3-p0 :002 > w.attempt => "wtf" Tuesday, October 2, 12
  8. class WTF include ICANHAZTYPOS def attempt "wtf" end end ruby-1.9.3-p0

    :001 > w = WTF.new => #<WTF:0x007ff7e0c74428> ruby-1.9.3-p0 :002 > w.attempt => "wtf" ruby-1.9.3-p0 :003 > w.atempt => "wtf" ruby-1.9.3-p0 :004 > w.attemp => "wtf" Tuesday, October 2, 12
  9. ruby-1.9.3-p0 :002 > w.winning? => [:attempt, :method_missing, :nil?, :===, :=~,

    :! ~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :! =, :instance_eval, :instance_exec, :__send__, :__id__] Tuesday, October 2, 12
  10. ruby-1.9.3-p0 :002 > w.winning? => [:attempt, :method_missing, :nil?, :===, :=~,

    :! ~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :! =, :instance_eval, :instance_exec, :__send__, :__id__] Queremos que EXPLOTE Leveshtein > 1 Tuesday, October 2, 12
  11. module ICANHAZTYPOS def method_missing(name, *args, &block) methods.each do |m| return

    self.send(m) if Levenshtein.distance(m.to_s, name.to_s) == 1 end end end Retorna los methods ya que nada paso en el loop Tuesday, October 2, 12
  12. it "booms for Levenshtein > 1" do expect { wtf.winning?

    }.to raise_error(NoMethodError) end Tuesday, October 2, 12
  13. module ICANHAZTYPOS def method_missing(name, *args, &block) methods.each do |m| return

    self.send(m) if Levenshtein.distance(m.to_s, name.to_s) == 1 end super end end Tuesday, October 2, 12
  14. ruby-1.9.3-p0 :002 > w.winning? NoMethodError: undefined method `winning?' for #<WTF:0x007f8392af3468>

    from /Users/ivan/Desktop/ ICANHAZTYPOS_spec.rb:9:in ` method_missing' Tuesday, October 2, 12