From: Jim Weirich Date: 2004-11-29T14:10:23+09:00 Subject: Re: Seeking advice on some method names > On Mon, 29 Nov 2004, Sam Stephenson wrote: > > Why not String? > > "Test::Unit".to_class # => Test::Unit On Sunday 28 November 2004 11:34 pm, David A. Black wrote: > I'm not sure what either of these adds to const_get -- or, to put it > another way, why there should be a special method to do (essentially) > a const_get only for strings that represent class names. Why? Because it is a common function that is easy to do wrong. The quick and obvious choices (eval(string) and Object.const_get(string)) are flawed. And the one-liner that does it correctly ... string.split("::").inject(Object) { |ns, n| ns.const_get(n) } ... is hardly transparent. It is a common enough activity that having a standard and canonical way of converting strings to classes would ease a number of tasks (e.g. anytime you wish to specify a class in a config file, input stream or data base). -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)