如何从ruby中的字符串名创建类实例?

时间:2021-08-28 23:22:32

I have the name of a class and I want to create an instance of that class so that I can loop through each rails attribute that is present in the schema of that class.

我有一个类的名称,我想要创建一个该类的实例,这样我就可以循环遍历该类模式中出现的每个rails属性。

How would I go about doing that?

我该怎么做呢?

  1. I have the name as a string of the class I want to check
  2. 我将名称作为要检查的类的字符串
  3. I guess I need to instantiate a class instance so that I can
  4. 我想我需要实例化一个类实例,这样我就可以
  5. Loop through it's attributes and print them.
  6. 循环它的属性并打印它们。

4 个解决方案

#1


169  

In rails you can just do:

在rails中,你可以这样做:

clazz = 'ExampleClass'.constantize

In pure ruby:

在纯ruby:

clazz = Object.const_get('ExampleClass')

with modules:

与模块:

module Foo
  class Bar
  end
end

you would use

您将使用

> clazz = 'Foo::Bar'.split('::').inject(Object) {|o,c| o.const_get c}
  => Foo::Bar 
> clazz.new
  => #<Foo::Bar:0x0000010110a4f8> 

#2


12  

Very simple in Rails: use String#constantize

Rails非常简单:使用字符串#constant

class_name = "MyClass"
instance = class_name.constantize.new

#3


5  

Try this:

试试这个:

Kernel.const_get("MyClass").new

Then to loop through an object's instance variables:

然后对对象的实例变量进行循环:

obj.instance_variables.each do |v|
  # do something
end

#4


3  

module One
  module Two
    class Three
      def say_hi
        puts "say hi"
      end
    end
  end
end

one = Object.const_get "One"

puts one.class # => Module

three = One::Two.const_get "Three"

puts three.class # => Class

three.new.say_hi # => "say hi"

In ruby 2.0 and, possibly earlier releases, Object.const_get will recursively perform a lookup on a namespaces like Foo::Bar. The example above is when the namespace is known ahead of time and highlights the fact that const_get can be called on modules directly as opposed to exclusively on Object.

在ruby 2.0和可能更早的版本中,对象。const_get将递归地对Foo: Bar之类的名称空间执行查找。上面的例子是这样的:名称空间是预先知道的,并且强调了一个事实,即const_get可以直接在模块上调用,而不是只在对象上调用。

#1


169  

In rails you can just do:

在rails中,你可以这样做:

clazz = 'ExampleClass'.constantize

In pure ruby:

在纯ruby:

clazz = Object.const_get('ExampleClass')

with modules:

与模块:

module Foo
  class Bar
  end
end

you would use

您将使用

> clazz = 'Foo::Bar'.split('::').inject(Object) {|o,c| o.const_get c}
  => Foo::Bar 
> clazz.new
  => #<Foo::Bar:0x0000010110a4f8> 

#2


12  

Very simple in Rails: use String#constantize

Rails非常简单:使用字符串#constant

class_name = "MyClass"
instance = class_name.constantize.new

#3


5  

Try this:

试试这个:

Kernel.const_get("MyClass").new

Then to loop through an object's instance variables:

然后对对象的实例变量进行循环:

obj.instance_variables.each do |v|
  # do something
end

#4


3  

module One
  module Two
    class Three
      def say_hi
        puts "say hi"
      end
    end
  end
end

one = Object.const_get "One"

puts one.class # => Module

three = One::Two.const_get "Three"

puts three.class # => Class

three.new.say_hi # => "say hi"

In ruby 2.0 and, possibly earlier releases, Object.const_get will recursively perform a lookup on a namespaces like Foo::Bar. The example above is when the namespace is known ahead of time and highlights the fact that const_get can be called on modules directly as opposed to exclusively on Object.

在ruby 2.0和可能更早的版本中,对象。const_get将递归地对Foo: Bar之类的名称空间执行查找。上面的例子是这样的:名称空间是预先知道的,并且强调了一个事实,即const_get可以直接在模块上调用,而不是只在对象上调用。