I’m a C++ programmer learning Ruby. In a beginner book I read:
“A class is itself an object, even if you don’t directly instantiate it.”
I don’t know how to interpret that.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In C++, with the exception of
typeidet al, there’s no language-visible runtime representation of a class. You can sayclass Foo {};, but then you can’t say&Foo. (You can get thetypeid/ address of thetypeinfoobject, which is a bastardized version of a class object).In Ruby, classes are objects. Anything you can do with an object, you can do with a class since it is an object. For example, in Ruby you can
foo.send(...)to any objectfoo. Since a class is an object, you can just as well asFoo.send(...).The part about “you didn’t instanciate it” refers to the fact that usually you say
foo = Foo.newin Ruby but you don’t need to say that for classes. The class object is created by the runtime. (In fact,class Foo; endin Ruby is pretty similar toFoo = Class.new.)