From: cldwalker Date: 2009-10-06T03:30:12+09:00 Subject: Re: method_missing for command shell? On Oct 5, 8:33 am, Roger Pack wrote: > Hi all.  My bash-fu is a little bit lacking, so here's my question. > > I'm wondering if anybody knows of a shell that can be programmed to > "guess" what you meant when you type in a command errantly, like (kind > of a bad example, but...) > > $ git dif > > (it runs git dif, fails, then, in method_missing style, I can tell it > "oh what I always means when I run git dif is git diff" so it correct > its for me and runs it). > > Any pointers on where to go there? I'm writing a ruby command/task execution gem which could allow for easy ruby wrapping of system commands and method_missing-fu for subcommands: http://github.com/cldwalker/boson I already do subcommand method_missing with boson, not for guessing but for smart aliasing: http://github.com/cldwalker/irbfiles/blob/master/boson/commands/boson_method_missing.rb#L10-25 Here's how you could define a simple Boson library that wraps git subcommands: module Git %w{add checkout diff rm merge pull push}.each do |e| define_method(e) do |*args| system('git', e, *args) end end end With boson you would be able to execute these subcommands from the commandline or irb as follows: bash> boson git.diff irb>> git.diff With the above method_missing library you could execute these as git.d or git.dif Unfortunately Boson isn't ready yet. Hopefully within the week. In the mean time you could take that example module and use it within irb: def git @git ||= Object.new.extend Git end class << git def method_missing(meth, *args, &block) if !(meths = Git.instance_methods.sort.grep(/^#{meth}/)).empty? send(meths[0].to_sym, *args, &block) else super end end end