From: rashworth Date: 2001-04-24T12:16:34+09:00 Subject: [ruby-talk:14129] Re: RCR: getClassFromString method What is the Ruby code to produce a 3 row by 2 column matrix of pixel elements, i.e. black and white? Thank you. Bob Ashworth ---------- Original Message ---------------------------------- From: "W. Kent Starr" Reply-To: ruby-talk@ruby-lang.org Date: Tue, 24 Apr 2001 12:03:23 +0900 >On Monday 23 April 2001 18:18, Marko Schulz wrote: >> On Tue, Apr 24, 2001 at 03:41:23AM +0900, Phil Tomson wrote: >> > In article <988034673.396094.26164.nullmailer@ev.netlab.zetabits.com>, >> > >> > Yukihiro Matsumoto wrote: >> > > [ Should it be encouraged style of programming to retrieve a class >> > > by its name in a string? ] >> > >> > 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. >> >> I would use a factory-method for this: >> >> class ToolFactory >> >> def ToolFactory.getTool >> case $PLATFORM >> when "Unix" then Tool_Unix >> when "PC" then Tool_PC >> else nil >> end >> end >> >> end >> >> >> I would favor passing the platform as a parameter since I don't like >> global variables. > >FYI Ruby provides this natively. > >ruby -e 'p RUBY_PLATFORM' -> "i586-linux" on my Redhat box. > >It will yield similar info on other platforms. The result can be parsed as in > >if RUBY_PLATFORM =~ /^(.*)linux$/ > >etc > >Hope tis helps. > >Regards, > >Kent Starr >elderburn@mindspring.com >