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?
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?
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.
If you want to test for an instance of a specific class, I'd go with something like:
@list.is_a?(List)
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
.