Dynamic Object Instantiation

T

Troy Denkinger

------=_Part_6773_210397.1141255788198
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I'm sure this question has been asked and answered in the past, but googlin=
g
and searching the list archives isn't getting me much. It's probably a cas=
e
of me not searching correctly. So, if someone has a pointer on that front,
let me know.

Basically, I want a class to dynamically load and instantiate objects.
Here's the code that is supposed to do that:

ComponentGatherer.rb
---------------------------------

class ComponentGatherer

def initialize()
@@components =3D []
Dir.foreach( File.dirname( __FILE__ ) ){|x|
next unless x =3D~ /component.rb$/i
x.gsub!( ".rb", "" )
require "#{x}"
@@components.push( x )
}
end

def components
@@components.each{ |x|
test =3D x.new()
# Blah, blah, blah
}
end
end


Locating and requring the files seems to be working fine, it's when the
component() call is made that I'm a bit lost. I have to
*Component.rbfiles, they look like this:

MyOtherComponent.rb
--------------------------------
class MyOtherTestComponent

attr :test

def initialize()
self.test =3D 2
end
end

The error I get from the components() method is:

undefined method `new' for "MyOtherTestComponent":String (NoMethodError)

I understand what the error is saying, but I'm not sure how to call new() o=
n
"x" above such that it returns an object of the requested class. Any help
is appreciated.

Regards,

Troy Denkinger

------=_Part_6773_210397.1141255788198--
 
J

Joel VanderWerf

Troy said:
I'm sure this question has been asked and answered in the past, but googling
and searching the list archives isn't getting me much. It's probably a case
of me not searching correctly. So, if someone has a pointer on that front,
let me know.

Basically, I want a class to dynamically load and instantiate objects.
Here's the code that is supposed to do that:

ComponentGatherer.rb
---------------------------------

class ComponentGatherer

def initialize()
@@components = []
Dir.foreach( File.dirname( __FILE__ ) ){|x|
next unless x =~ /component.rb$/i
x.gsub!( ".rb", "" )
require "#{x}"
@@components.push( x )
}
end

def components
@@components.each{ |x|
test = x.new()
# Blah, blah, blah
}
end
end


Locating and requring the files seems to be working fine, it's when the
component() call is made that I'm a bit lost. I have to
*Component.rbfiles, they look like this:

MyOtherComponent.rb
--------------------------------
class MyOtherTestComponent

attr :test

def initialize()
self.test = 2
end
end

The error I get from the components() method is:

undefined method `new' for "MyOtherTestComponent":String (NoMethodError)

I understand what the error is saying, but I'm not sure how to call new() on
"x" above such that it returns an object of the requested class. Any help
is appreciated.

Regards,

Troy Denkinger

If there is a way to map from the file name to the class name (in this
example, it would map "MyOtherComponent.rb" to "MyOtherTestComponent"),
then you can do this first, and then use const_get to get the class:

x = "MyOtherComponent.rb"
x.gsub(/.rb$/, "")
x.gsub(/Component/, "TestComponent")
cl = Kernel.const_get x
obj = cl.new

But it's sometimes difficult to enforce this filename/classname consistency.

I prefer something like this:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/181735

It still uses const_get, but it wraps the component inside a module, so
that the global namespace doesn't get polluted, and you can query for
the class that was defined in a file without mucking around with the
file name.

HTH.
 
T

Troy Denkinger

------=_Part_9484_13175311.1141409740618
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Thanks for the followup. I also figured out how to search and found this:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/13414?help-en

Which also helps.

Thanks.

Troy said:
I'm sure this question has been asked and answered in the past, but googling
and searching the list archives isn't getting me much. It's probably a case
of me not searching correctly. So, if someone has a pointer on that front,
let me know.

Basically, I want a class to dynamically load and instantiate objects.
Here's the code that is supposed to do that:

ComponentGatherer.rb
---------------------------------

class ComponentGatherer

def initialize()
@@components =3D []
Dir.foreach( File.dirname( __FILE__ ) ){|x|
next unless x =3D~ /component.rb$/i
x.gsub!( ".rb", "" )
require "#{x}"
@@components.push( x )
}
end

def components
@@components.each{ |x|
test =3D x.new()
# Blah, blah, blah
}
end
end


Locating and requring the files seems to be working fine, it's when the
component() call is made that I'm a bit lost. I have to
*Component.rbfiles, they look like this:

MyOtherComponent.rb
--------------------------------
class MyOtherTestComponent

attr :test

def initialize()
self.test =3D 2
end
end

The error I get from the components() method is:

undefined method `new' for "MyOtherTestComponent":String (NoMethodError)

I understand what the error is saying, but I'm not sure how to call new() on
"x" above such that it returns an object of the requested class. Any help
is appreciated.

Regards,

Troy Denkinger

If there is a way to map from the file name to the class name (in this
example, it would map "MyOtherComponent.rb" to "MyOtherTestComponent"),
then you can do this first, and then use const_get to get the class:

x =3D "MyOtherComponent.rb"
x.gsub(/.rb$/, "")
x.gsub(/Component/, "TestComponent")
cl =3D Kernel.const_get x
obj =3D cl.new

But it's sometimes difficult to enforce this filename/classname
consistency.

I prefer something like this:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/181735

It still uses const_get, but it wraps the component inside a module, so
that the global namespace doesn't get polluted, and you can query for
the class that was defined in a file without mucking around with the
file name.

HTH.

------=_Part_9484_13175311.1141409740618--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top