From: ara.t.howard@... Date: 2006-02-24T01:13:29+09:00 Subject: Re: Converting a string to a class On Fri, 24 Feb 2006, James Edward Gray II wrote: > On Feb 23, 2006, at 9:07 AM, Anthony DeRobertis wrote: > >> Gregory Brown wrote: >> >>> On 2/22/06, Anthony DeRobertis wrote: >>>> Take the following snippet: >>> >>> why do string conversion? >>> >>> You can pass constants around in ruby just fine. >> >> I'm scanning a directory for plugins and loading them. When I load a >> file called, for example, some_plugin.rb, I assume it creates a class >> Whatever::Plugins::SomePlugin, which I naturally only have as a >> string... > > You can do this without using the name assumptions in Ruby, if you give them > a class to inherit from. Classes get notified when they are subclassed: > >>> class Plugin >>> def self.inherited( plugin_class ) >>> (@@plugins ||= Array.new) << plugin_class >>> end >>> def self.loaded >>> @@plugins || Array.new >>> end >>> end > => nil >>> class FirstPlugin < Plugin; end > => nil >>> class SecondPlugin < Plugin; end > => nil >>> Plugin.loaded > => [FirstPlugin, SecondPlugin] > > Hope that gives you some fresh ideas. i've abstracted this pattern as the 'dynaload' library here http://rubyforge.org/frs/?group_id=1024&release_id=3786 regards. -a -- judge your success by what you had to give up in order to get it. - h.h. the 14th dali lama