来自其对象的类名称

发布于 2024-08-04 23:36:09 字数 80 浏览 2 评论 0原文

如何从类的对象中提取类的名称?

例如,我有一个 @list 对象,我知道它肯定是 List 类的实例。但如何直接在代码中提取它呢?

How to extract a name of the class from its object?

For example I have a @list object which I know is surely an instance of List class. But how do I extract that directly in code?

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

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

发布评论

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

评论(3

中二柚 2024-08-11 23:36:10

就像Edwin所说,object.class会给你相应的Class对象。如果您只需要类的名称,请使用object.class.name

Like Edwin said, object.class will give you the corresponding Class object. If you just want the name of the class, use object.class.name.

昔梦 2024-08-11 23:36:09

此类信息是相当基本的 Ruby 编程。答案是:

object.class

下次额外提示:尝试自己在核心库文档中查找此信息。您知道您有某种对象,只需开始阅读文档,您就会找到一些适合您需求的方法。有关可对对象执行的方法的信息可以在此处找到。

This kind of information is rather basic Ruby programming. The answer is:

object.class

Extra tip for the next time: try finding this information yourself in the core library documentation. You know you have some kind of object, just start reading the documentation and you will find some method that suites your needs. Information about the methods you can perform on an object can be found here.

甜`诱少女 2024-08-11 23:36:09

如果您想测试特定类的实例,我会选择以下内容:

@list.is_a?(List)

If you want to test for an instance of a specific class, I'd go with something like:

@list.is_a?(List)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文