tag:blogger.com,1999:blog-3718956085911858962.post277306284681829738..comments2023-12-27T14:48:02.113+05:30Comments on Electric Sheep Blog: Loading classes from strings in RubyAnonymoushttp://www.blogger.com/profile/11938300811286150164noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-3718956085911858962.post-42942411739949362972010-08-31T00:23:04.210+05:302010-08-31T00:23:04.210+05:30Sidu and Travis: thanks for clarifying this. I am...Sidu and Travis: thanks for clarifying this. I am able to use Travis' way to retrieve a class nested within a module.Scott Smithhttp://blog.scottnelsonsmith.comnoreply@blogger.comtag:blogger.com,1999:blog-3718956085911858962.post-56276181814304174182010-06-14T23:16:56.792+05:302010-06-14T23:16:56.792+05:30I haven't benchmarked this, but you can also d...I haven't benchmarked this, but you can also do:<br /><br />Module.const_get("ModuleName").const_get("ClassName")Travishttps://www.blogger.com/profile/01398330633165910535noreply@blogger.comtag:blogger.com,1999:blog-3718956085911858962.post-14482611432840022432010-03-22T07:49:00.428+05:302010-03-22T07:49:00.428+05:30I added the Rails 2.3.5 constantize to the benchma...I added the Rails 2.3.5 constantize to the benchmarks above, and in my results it's over 7 times slower than the "simple invocation" and 4 times slower than const_get.Mike Boonehttp://boonedocks.net/mikenoreply@blogger.comtag:blogger.com,1999:blog-3718956085911858962.post-49513362371635107722009-10-26T01:59:06.854+05:302009-10-26T01:59:06.854+05:30Yes, I have!!! But #constantize is a Rails (active...Yes, I have!!! But #constantize is a Rails (activesupport) extension, so without Rails, no #constantize.Anonymoushttps://www.blogger.com/profile/11938300811286150164noreply@blogger.comtag:blogger.com,1999:blog-3718956085911858962.post-59531595184529480562009-10-25T09:04:29.211+05:302009-10-25T09:04:29.211+05:30Have you tried "MyClassName".constantize...Have you tried "MyClassName".constantize.new ???Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-3718956085911858962.post-91509810137609714872009-06-04T08:58:07.671+05:302009-06-04T08:58:07.671+05:30If you have a string containing a potential class ...If you have a string containing a potential class name and you're not sure if its formatted correctly for whatever reason before you use 'eval' or 'Kernel.const_get', you can use 'Kernel.subclasses_of(ActiveRecord::Base).member?(YourClass)' to check if it exists so you don't get a 'LoadError: ./activesupport-2.3.2/lib/active_support/dependencies.rb:426:in `load_missing_constant''Dave Rogershttps://www.blogger.com/profile/03159006525227734356noreply@blogger.comtag:blogger.com,1999:blog-3718956085911858962.post-87138953227323667682008-04-17T04:56:00.000+05:302008-04-17T04:56:00.000+05:30Not exactly faster, but another approach is to ite...Not exactly faster, but another approach is to iterate through the ObjectSpace collection, and instantiate the matching class:<BR/><BR/>require 'my_package/my_class'<BR/><BR/>class Factory<BR/> def self.get_object(o)<BR/> ObjectSpace.each_object(Class) do |c|<BR/> return c.new() if c.name.eql?(o)<BR/> end<BR/> return nil<BR/> end<BR/>end<BR/><BR/>#usage<BR/>require 'factory'<BR/>o = Factory.get_object("MyPackage::MyClass")/dev/noelhttps://www.blogger.com/profile/03017211122191024916noreply@blogger.comtag:blogger.com,1999:blog-3718956085911858962.post-47445537962818328372008-02-07T07:38:00.000+05:302008-02-07T07:38:00.000+05:30I've been experimenting with JRuby and found that ...I've been experimenting with JRuby and found that using const_get is just unreliable, while eval works fine. For example, a java class com.foo.bar.Baz is represented as Java::FooBar::Baz. If you do const_get on Java::FooBar, you get a string "Java::FooBarConst_get". It appears that these Java package modules must define method_missing to just concatenate the method name to the module name. I haven't investigated further.<BR/><BR/>Anyway, using eval works fine.Dean Wamplerhttps://www.blogger.com/profile/02505288380679936730noreply@blogger.com