An aggressive spellchecker for method calls
GuessMethod is probably a bad idea:
mvb:~ cms$ irb 001:0> load 'mm.rb' true 002:0> class Object; include GuessMethod; end Object 003:0> ['1', '2', '3'].mp {|x| x.toi} attention: sending map instead of mp to ["1", "2", "3"]:Array attention: sending to_i instead of toi to "1":String attention: sending to_i instead of toi to "2":String attention: sending to_i instead of toi to "3":String [1, 2, 3] 004:0> ['1','2','3'].mp {|x| x.to_} attention: sending map instead of mp to ["1", "2", "3"]:Array ambiguous method to_, possible matches to_a, to_i, to_s, to_f for "1":String NoMethodError: undefined method `to_' for "1":String from ./mm.rb:9:in `old_method_missing' from ./mm.rb:34:in `method_missing' from (irb):4 from ./mm.rb:25:in `map' from ./mm.rb:25:in `send' from ./mm.rb:25:in `method_missing' from (irb):4 from (irb):3 005:0> eixt attention: sending exit instead of eixt to main:Object 13 seconds mvb:~ cms$
It’s using the method_missing pre-handling trick I talked about earlier. It’s nasty. I’m not actually actively using this, but I’ve enjoyed toying around with it.
You can see after line 4 that the ambiguous ‘to_’ gets passed on to String’s method_missing, which would be cool if String had a method_missing that did something interesting. But this is a bad idea anyway. The whole thing is one giant bad idea.
Dr Nic said,
July 17, 2007 @ 11:04 pm
Its an awesome idea. I toyed around with this idea under the name Magic Wiggly Line, in reference to the wiggly lines you get with spellcheckers for incorrect words. Have you released the code? I’ll use it in test + dev.
Chris Shea said,
July 18, 2007 @ 1:17 pm
I’m going to release the code. There are a couple of options I’d like to make available first. I’ll put it up on rubyforge and announce it here soon.
Dr Nic said,
July 19, 2007 @ 2:46 am
Does it work for constants too? (via const_missing)
Chris Shea said,
July 19, 2007 @ 9:56 am
Dr, not in the first release, though that’s a great idea. I’m hoping to get it out the door real soon.