How to Get the Class Name of a Ruby Object?

To get the name of an object's class in Ruby, you can simply use, for example, like so:

print [] #=> Array
print "" #=> String
# ...

If your class is a part of a module, then the output for would also include the module name. For example:

module Shape
  class Triangle

triangle =

print #=> Shape::Triangle

To only get the class name, you can remove the module name, for example, like so:

print"::").last #=> Triangle

If you're using Rails, then you may use the demodulize method to achieve the same:

print #=> Triangle

Hope you found this post useful. It was published . Please show your love and support by sharing this post.