From: ptkwt@...1.aracnet.com (Phil Tomson) Date: 2001-04-24T03:41:23+09:00 Subject: [ruby-talk:14098] Re: RCR: getClassFromString method In article <988034673.396094.26164.nullmailer@ev.netlab.zetabits.com>, Yukihiro Matsumoto wrote: >Hi, > >In message "[ruby-talk:14040] RCR: getClassFromString method" > on 01/04/23, Phil Tomson writes: > >|It would be nice to have a function that returns a class type given a >|string that names that class. I realize that this is doable already in at >|least a couple of different ways - you can use the each_object method in >|ObjectSpace to iterate through all of the classes till you find the one >|you need, or you can use const_get in Modules, (at least that's where I >|think it is), but none of those ways is intuitive and easy to figure out >|for newbies. > >You are right. None of them is intuitive and easy to fugure out. But >think about this: Should this really be encouraged style of programming? Well, perhaps it shouldn't be encouraged, I don't know. I'm not necessarily an OO purist, I suppose. I know that I use this feature in a certain case where I have a class hierarchy like: class Tool #methods to handle generic stuff common to PC and UNIX end class Tool_PC < Tool #methods do PC specific stuff end class Tool_UNIX < Tool #methods do UNIX related stuff end Let's say that early on in the script I figure out what platform I'm running on, say: $PLATFORM = "PC" ($PLATFORM is global so I can instantiate Tool_"$PLATFORM" objects anywhere later on in the script) Then I can instantiate the correct class based on the value of PLATFORM There are probably other ways to do this, but getting the Class from a String seems pretty straitforward in this case - and it is how I've implemented this. So maybe it's not a good 'pure' thing to allow, but it does have its uses. Phil