From: Intransition Date: 2010-07-06T08:29:20+09:00 Subject: Golf on CLI code I have a registry (array) of CLI classes. Each class has a class method cli() that returns an array of strings or regular expressions to match against ARGV to see if it is the chosen cli. Eg. class FooList << CLIBase def self.cli ; ['foo', 'list'] ; end ... end So if someone puts on the command line: $ foo list Then the above class would match for it. I wrote the algorithm to handle this, but I think it's Ugh-ly. Wondering if the smart folk here might play a game of golf on it. def command(argv) lookup = registry.map{ |cc| [cc, cc.cli] }.sort{ |a,b| b[1].size <=> a[1].size } x = lookup.find do |cc, cli| t = true cli.each_with_index do |r, i| t = t && (r === argv[i]) end t end cmdc = x[0] argv = argv[x[1].size..-1] return cmdc, argv end