Forum: Ruby Idiom for dynamic loading of an unknown class from a known f

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nasir K. (Guest)
on 2007-02-10 23:30
(Received via mailing list)
I need to load a class at runtime, whose name I do not know in advance,
but
all I know is a file name.
Say "a.rb" which is


class A
  ......
end

So I want to do something like  (pseudo code)
 - load a.rb  (this will create the class object for A)
 - Somehow access constant "A" and invoke "A.new"

I can think of a few hacks but none of them are elegant.
Is there an idiom for it?


TIA
unknown (Guest)
on 2007-02-10 23:53
(Received via mailing list)
On Sun, 11 Feb 2007, Nasir K. wrote:

> - load a.rb  (this will create the class object for A)
> - Somehow access constant "A" and invoke "A.new"
>
> I can think of a few hacks but none of them are elegant.
> Is there an idiom for it?
>
>

dynaload does __exactly__ this.

gem install dynaload

-a
Joel VanderWerf (Guest)
on 2007-02-10 23:57
(Received via mailing list)
Nasir K. wrote:
> - load a.rb  (this will create the class object for A)
> - Somehow access constant "A" and invoke "A.new"
>
> I can think of a few hacks but none of them are elegant.
> Is there an idiom for it?

# http://redshift.sourceforge.net/script
require 'script'
script = Script.load "a.rb"
script::A.new
This topic is locked and can not be replied to.