如何检查一个类是否已定义?

发布于 2024-11-02 18:52:47 字数 638 浏览 2 评论 0原文

如何将字符串转换为类名,但前提是该类已经存在?

如果 Amber 已经是一个类,我可以通过以下方式从字符串获取该类:

Object.const_get("Amber")

或 (在 Rails 中)

"Amber".constantize

但是,如果以下情况中的任何一个都将失败,并显示 NameError: uninitializedconstant Amber Amber 还不是一个类。

我的第一个想法是使用 define? 方法,但它不会区分已经存在的类和不存在的类:

>> defined?("Object".constantize)
=> "method"
>> defined?("AClassNameThatCouldNotPossiblyExist".constantize)
=> "method"

那么,在我尝试之前,如何测试字符串是否命名了一个类转换它? (好吧,用 begin/rescue 块来捕获 NameError 错误怎么样?太丑了?我同意......)

How do I turn a string into a class name, but only if that class already exists?

If Amber is already a class, I can get from a string to the class via:

Object.const_get("Amber")

or (in Rails)

"Amber".constantize

But either of these will fail with NameError: uninitialized constant Amber if Amber is not already a class.

My first thought is to use the defined? method, but it doesn't discriminate between classes that already exist and those that don't:

>> defined?("Object".constantize)
=> "method"
>> defined?("AClassNameThatCouldNotPossiblyExist".constantize)
=> "method"

So how do I test if a string names a class before I try to convert it? (Okay, how about a begin/rescue block to catch NameError errors? Too ugly? I agree...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空城之時有危險 2024-11-09 18:52:47

const_define 怎么样?

请记住,在 Rails 中,开发模式下会自动加载,因此在测试时可能会很棘手:

>> Object.const_defined?('Account')
=> false
>> Account
=> Account(id: integer, username: string, google_api_key: string, created_at: datetime, updated_at: datetime, is_active: boolean, randomize_search_results: boolean, contact_url: string, hide_featured_results: boolean, paginate_search_results: boolean)
>> Object.const_defined?('Account')
=> true

How about const_defined??

Remember in Rails, there is auto-loading in development mode, so it can be tricky when you are testing it out:

>> Object.const_defined?('Account')
=> false
>> Account
=> Account(id: integer, username: string, google_api_key: string, created_at: datetime, updated_at: datetime, is_active: boolean, randomize_search_results: boolean, contact_url: string, hide_featured_results: boolean, paginate_search_results: boolean)
>> Object.const_defined?('Account')
=> true
忘年祭陌 2024-11-09 18:52:47

在 Rails 中,这非常简单:

amber = "Amber".constantize rescue nil
if amber # nil result in false
    # your code here
end

In rails it's really easy:

amber = "Amber".constantize rescue nil
if amber # nil result in false
    # your code here
end
对你的占有欲 2024-11-09 18:52:47

受到@ctcherry 上面的响应的启发,这里有一个“安全类方法发送”,其中 class_name 是一个字符串。如果class_name没有命名类,则返回nil。

def class_send(class_name, method, *args)
  Object.const_defined?(class_name) ? Object.const_get(class_name).send(method, *args) : nil
end

一个更安全的版本,仅当 class_name 响应时才调用 method

def class_send(class_name, method, *args)
  return nil unless Object.const_defined?(class_name)
  c = Object.const_get(class_name)
  c.respond_to?(method) ? c.send(method, *args) : nil
end

Inspired by @ctcherry's response above, here's a 'safe class method send', where class_name is a string. If class_name doesn't name a class, it returns nil.

def class_send(class_name, method, *args)
  Object.const_defined?(class_name) ? Object.const_get(class_name).send(method, *args) : nil
end

An even safer version which invokes method only if class_name responds to it:

def class_send(class_name, method, *args)
  return nil unless Object.const_defined?(class_name)
  c = Object.const_get(class_name)
  c.respond_to?(method) ? c.send(method, *args) : nil
end
最初的梦 2024-11-09 18:52:47

看来使用 Object.const_define? 方法的所有答案都是有缺陷的。如果由于延迟加载而尚未加载相关类,则断言将失败。明确实现这一目标的唯一方法如下:

  validate :adapter_exists

  def adapter_exists
    # cannot use const_defined because of lazy loading it seems
    Object.const_get("Irs::#{adapter_name}")
  rescue NameError => e
    errors.add(:adapter_name, 'does not have an IrsAdapter')
  end

It would appear that all the answers using the Object.const_defined? method are flawed. If the class in question has not been loaded yet, due to lazy loading, then the assertion will fail. The only way to achieve this definitively is like so:

  validate :adapter_exists

  def adapter_exists
    # cannot use const_defined because of lazy loading it seems
    Object.const_get("Irs::#{adapter_name}")
  rescue NameError => e
    errors.add(:adapter_name, 'does not have an IrsAdapter')
  end
丢了幸福的猪 2024-11-09 18:52:47

我创建了一个验证器来测试字符串是否是有效的类名(或以逗号分隔的有效类名列表):

class ClassValidator < ActiveModel::EachValidator
  def validate_each(record,attribute,value)
    unless value.split(',').map { |s| s.strip.constantize.is_a?(Class) rescue false }.all?
      record.errors.add attribute, 'must be a valid Ruby class name (comma-separated list allowed)'
    end
  end
end

I've created a validator to test if a string is a valid class name (or comma-separated list of valid class names):

class ClassValidator < ActiveModel::EachValidator
  def validate_each(record,attribute,value)
    unless value.split(',').map { |s| s.strip.constantize.is_a?(Class) rescue false }.all?
      record.errors.add attribute, 'must be a valid Ruby class name (comma-separated list allowed)'
    end
  end
end
丢了幸福的猪 2024-11-09 18:52:47

另一种方法,以防你也想上这门课。如果类未定义,将返回 nil,因此您不必捕获异常。

class String
  def to_class(class_name)
    begin
      class_name = class_name.classify (optional bonus feature if using Rails)
      Object.const_get(class_name)
    rescue
      # swallow as we want to return nil
    end
  end
end

> 'Article'.to_class
class Article

> 'NoSuchThing'.to_class
nil

# use it to check if defined
> puts 'Hello yes this is class' if 'Article'.to_class
Hello yes this is class

Another approach, in case you want to get the class too. Will return nil if the class isn't defined, so you don't have to catch an exception.

class String
  def to_class(class_name)
    begin
      class_name = class_name.classify (optional bonus feature if using Rails)
      Object.const_get(class_name)
    rescue
      # swallow as we want to return nil
    end
  end
end

> 'Article'.to_class
class Article

> 'NoSuchThing'.to_class
nil

# use it to check if defined
> puts 'Hello yes this is class' if 'Article'.to_class
Hello yes this is class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文