From: Charles Comstock Date: 2004-07-08T08:17:34+09:00 Subject: Re: Class#=== has interesting results > Ack. > > And a question to the OP: why do you want to apply the case statement to > classes instead of to their instances? What should this case statement > do (depending on which class is given)? Maybe we can find an alternative > to what you think you need. > > Regards, > Pit > > def parse(str) input = str.chomp.split comm = CommandFactory[input[0]] case comm when StartCommand comm.new(input[1],input[2..-1]) @mobile = input[1] when HelpCommand else comm.new(@mobile,input[1..-1]) end end CommandFactory returns the name of the class so that can instantiate it later with different arguments. Normally all commands reference who they are from but the start command and help are different since the start command defines who it is from, and the help command needs to be accessable before and after the start command has actually instantiated a character. I could pass these as parameters later on through a different function, but this seemed a logical way to do it. I think this should work: def parse(str) input = str.chomp.split comm = CommandFactory[input[0]] case comm.id when StartCommand.id comm.new(input[1],input[2..-1]) @mobile = input[1] when HelpCommand.id else comm.new(@mobile,input[1..-1]) end end Since we are matching id's now which is valid. The command factory part is actually pretty slick too, there is a Command superclass and whenever anything derives from it, self.inherited gets called on Command, which then grabs the singleton instance of CommandFactory and adds the inherited class name to a hash in CommandFactory. So CommandFactory has a hash which amoung other things includes {"start" => StartCommand, "help" => HelpCommand}, so when I parse the commandline I just grab the needed command right out of the CommandFactory and then instantiate it with the rest of the commandline parameters. It's just a stupid mud project I am doing to test out pattern based software design. When it's finished I'll release it to the community as example code. I think id should fix my problem, it just suprised me when it didn't match the name like I thought it would. Charles Comstock