如何获取 Ruby 类的名称? [英] How do I get the name of a Ruby class?
本文介绍了如何获取 Ruby 类的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 ActiveRecord 对象获取类名?
How can I get the class name from an ActiveRecord object?
我有:
result = User.find(1)
我试过了:
result.class
# => User(id: integer, name: string ...)
result.to_s
# => #<User:0x3d07cdc>"
我只需要字符串中的类名(在本例中为 User
).有没有办法做到这一点?
I need only the class name, in a string (User
in this case). Is there a method for that?
我知道这是非常基本的,但我搜索了 Rails 和 Ruby 的文档,但找不到.
I know this is pretty basic, but I searched both Rails' and Ruby's docs, and I couldn't find it.
推荐答案
您想调用 .name:
result.class.name
这篇关于如何获取 Ruby 类的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文