From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2001-04-26T06:11:23+09:00 Subject: [ruby-talk:14242] Re: RCR: getClassFromString method In article <200104240346.MAA25348@mx.inac.co.jp>, TAKAHASHI Masayoshi wrote: >Hi, > >I like using Hash rather than case-when dispatching. > > class ToolFactory > > @@platform2tool = { > "Unix" => Tool_Unix, > "PC" => Tool_PC > } > > def ToolFactory.getTool > @@platform2tool[$PLATFORM] > end > > end > >If you would like to use ruby's native platform information >as [ruby-talk:14126], you could use Regexp as key. > > class ToolFactory > > @@platform2tool = [ > [/^(.*)linux$/, Tool_Linux], > [/solaris/, Tool_Solaris] > ] > > def ToolFactory.getTool > @@platform2tool.each{|platform, klass| > if platform =~ RUBY_PLATFORM > return klass > end > } > return nil > end > > end I do like this approach. The original approach I posted was a translation from Perl - but this is much nicer. So perhaps it isn't absolutely neccessary to have a 'getClassFromString' method. Phil