7

所以我遇到了这种情况,我想以编程方式定义一堆类。我在下面使用的方法工作正常,除了我从这里继承的第 3 方类不喜欢该类是匿名定义的事实(基本上,它没有它需要的信息,即.类名,在“继承”钩子中,在我有机会将匿名类设置为常量之前)。

['one', 'two', 'three'].each do |model|
    cls = Class.new(ThirdPartyClass) do
        define_method :model do
            model
        end
    end
    ThirdPartyClass.const_set(model.capitalize, cls)
end

我可以简单地使用 eval 并定义类,例如:

['one', 'two', 'three'].each do |model|
    eval "class ThirdPartyClass::#{model.capitalize} < ThirdPartyClass; ...; end"
end

但我不喜欢它,因为它是混乱的字符串插值。从美学上讲,基于块的方法似乎“更好”。

有什么方法可以使用基于块的语法非匿名地定义该类(即从一开始就有一个名称),还是我注定要留在 eval 混乱的字符串输入领域?

4

1 回答 1

0

创建类Class#inherited时调用回调。当实例化一个匿名类时,这总是在类可以被分配给常量之前,给它一个名字。我看不到任何解决此问题的方法。您可以通过使用 eval 使用特殊的类语法创建一个空类来最小化字符串 eval 中的代码量,该类语法使名称在之前可见inherited,然后以块形式跟进 class_eval 来定义您的方法。

class Super
  def self.inherited(child)
    puts "#{self.name} inherited by #{child.inspect} named '#{child.name}'"
  end
end

# Your way, inherited can't see name
['one', 'two', 'three'].each do |model|
  klassname = model.capitalize
  klass = Class.new(Super) do
    puts "defining #{model} inheriting from Super"
    define_method :model do
      model
    end
  end
  Super.const_set(klassname, klass)
end

# this way inherited sees name because we use special class definition syntax in minimal string eval
['four', 'five', 'six'].each do |model|
  klassname = model.capitalize

  eval %Q{
    class Super::#{klassname} < Super
      puts %Q[defining Super::#{klassname} inheriting from Super]
    end
  }

  Super.const_get(klassname).class_eval do
    puts "defining methods for Super::#{klassname} inheriting from Super"
    define_method :model do
      model
    end
  end
end

# produces:
Super inherited by #<Class:0x0000010084c988> named ''
defining one inheriting from Super
Super inherited by #<Class:0x0000010084c640> named ''
defining two inheriting from Super
Super inherited by #<Class:0x0000010084c320> named ''
defining three inheriting from Super
Super inherited by Super::Four named 'Super::Four'
defining Super::Four inheriting from Super
defining methods for Super::Four inheriting from Super
Super inherited by Super::Five named 'Super::Five'
defining Super::Five inheriting from Super
defining methods for Super::Five inheriting from Super
Super inherited by Super::Six named 'Super::Six'
defining Super::Six inheriting from Super
defining methods for Super::Six inheriting from Super
于 2013-01-16T19:52:38.900 回答