From: Jeff Mitchell Date: 2004-07-07T18:38:18+09:00 Subject: Re: Class#=== has interesting results --- Charles Comstock wrote: > > > > whatisit = case obj > > when Array > > "it's an array!" > > when Hash > > "it's a hash!" > > when Septuagenarian > > "it's a septuagenarian!" > > end > > > > $ ri Module#=== > > ------------------------------------------------------------- Module#=== > > mod === obj => true or false > > ------------------------------------------------------------------------ > > Case Equality---Returns +true+ if _anObject_ is an instance of > > _mod_ or one of _mod_'s descendents. Of limited use for modules, > > but can be used in +case+ statements to classify objects by class. > > > > Right I was trying to use it in an example almost exactly like that > except that obj was a name of a commandclass. > > Ie: > 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 > > The idea was for a command factory to return the name of the command, > and instantiate that command with the rest of the string as parameters, > except for a few special case examples which would be checked against in > a case statement. Since StartCommand === StartCommand returns false > this doesn't work. Why you would want == to return true and === to > return false in this case is beyond me. I knew about the example you > gave above, that was what I was trying to emulate except at a step before. I'm not sure what you mean. StartCommand is not and instance of StartCommand or one of its decendants, therefore StartCommand === StartCommand returns false. Did you want the case statement behave differently different when given a class? Does this help: case comm.to_s.intern when :StartCommand comm.new(input[1],input[2..-1]) @mobile = input[1] when :HelpCommand else comm.new(@mobile,input[1..-1]) end __________________________________ Do you Yahoo!? New and Improved Yahoo! Mail - 100MB free storage! http://promotions.yahoo.com/new_mail