我正在尝试创建一个新类,它将从ActiveRecord::Base
继承,该类需要从字符串中动态生成
"general_systems".camelize.singularize = Class.new < ActiveRecord::Base
但是我一直收到错误:
undefined method `singularize=' for "GeneralSystems":String
我也试过constantize
字符串
>> foo = "general_systems".camelize.singularize
=> "GeneralSystem"
>> foo.constantize
NameError: uninitialized constant GeneralSystem
from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/inflector/methods.rb:124:in `block in constantize'
from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/inflector/methods.rb:123:in `each'
from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/inflector/methods.rb:123:in `constantize'
from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/string/inflections.rb:43:in `constantize'
from (irb):4
from /usr/bin/irb:12:in `<main>'
>> foo.constantize = Class.new
NoMethodError: undefined method `constantize=' for "GeneralSystem":String
from (irb):5
from /usr/bin/irb:12:in `<main>'
非常感谢任何帮助。
答案 0 :(得分:33)
如果您正在使用Rails,它会提供一个名为#constantize
的方法,该方法将起作用:
irb(main):017:0> Object.const_get 'House::Owns'
NameError: wrong constant name House::Owns
'House::Owns'.constantize
=> House::Owns
答案 1 :(得分:32)
这样的东西?
>> Object.const_set("general_systems".classify, Class.new)
=> GeneralSystem
>> GeneralSystem.new
=> #<GeneralSystem:0x105b0f738>
答案 2 :(得分:13)
klazz = Class.new(ActiveRecord::Base) do
def do_something_fun(param1)
param1.have_fun!
end
end
klazz_name = "general_systems".singularize.classify
Object.const_set(klazz_name, klazz)
答案 3 :(得分:8)
从Ruby Book安装程序中包含的“The Book of Ruby”中查看此示例。
puts("What shall we call this class?> ")
className = gets.strip().capitalize()
Object.const_set(className,Class.new)
puts("I'll give it a method called > 'myname'" )
className = Object.const_get(className)
className::module_eval{
define_method(:myname){
puts("The name of my class is '#{self.class}'" )
} }
x = className.new x.myname
答案 4 :(得分:4)
如果您的字符串包含命名空间,您可以使用:
require 'rubygems'
require 'active_support/inflector'
parent=String # using String to get a self contained example
# require 'active_record' # uncomment for ActiveRecord::Base as parent class
# parent=ActiveRecord::Base # uncomment for ActiveRecord::Base as parent class
namespace = 'A::B::general_systems'.split('::')
class_name = namespace.pop
namespace = namespace.inject(Object) do |mod, name|
if mod.constants.collect{|sym| sym.to_s}.include? name.classify
mod.const_get name.classify
else
mod.const_set name.classify, Module.new
end
end
klass = if namespace.constants.include? class_name.classify
namespace.const_get class_name.classify
else
namespace.const_set class_name.classify, Class.new(parent)
end
object = klass.allocate # allocate new object of klass
object.send :initialize # initialize object
object.class
object.class.superclass
答案 5 :(得分:1)
给出:
class Banana
end
您可以使用以下方式在纯Ruby中获取该类:
Object.const_get 'Banana'
=> Banana
或者如果您使用的是Rails:
'Banana'.constantize
=> Banana
答案 6 :(得分:0)
试
>> "general_systems".classify
=> "GeneralSystem"