So… let’s suppose you’re hacking away in Ruby and you mistype a method name:
irb(main):001:0> 5.clas NoMethodError: undefined method `clas' for 5:Fixnum from (irb):1
Obviously I *meant* to type ‘class’, but I got the arity (0) right and the spelling wrong.
I propose an extension to Ruby based on the hypothesis that typos are more common than arity errors: if I call an undefined method with arity A on an object then I probably meant to call *one* of the object’s methods of arity A. Just printing an error message is pretty doof since its certainly not what you wanted to happen. A statistically much better solution is to pick one method of the correct arity at random and execute that — it’s going to be right *some* of the time!
How would we do this in Ruby? After little bit of experimentation I produced this:
class Object def method_missing(name, *args) choices = self.class.instance_methods.collect do |name| self.method name end right_arity = choices.select do |method| method.arity == args.length end which = right_arity[rand(right_arity.length)] which.call(*args) end end
Just include this in your program and *all* Ruby objects will have this amazing feature!!!!
irb(main):017:0> 4.asdsa nil irb(main):018:0> 4.asdsa 4 irb(main):019:0> 4.asdsa Fixnum irb(main):020:0> 4.asdsa -5 irb(main):023:0> 4.asdsa(7) 11
:D :D :D