class Person
def name
puts "Dave"
end
end
puts Person.object_id
There are only two ways of accessing methods :
1) Someclass.method in case of class methods. #where Someclass is a class.
2) and Object.method when the method being accessed is a regular method declared inside a class. and Object is an instance of a class.
It follows the pattern Object.method so, does it mean Person class is really an object?
or object_id is a class method? The latter seems unlikely because class methods cannot be inherited into an instance. but when we do something like this :
a = Person.new
a.methods.include?("object_id") # this produces true
a is an instance of Person class so object_id cannot be a class method.
Yes, classes in Ruby are instances of class
Class
. In fact, you can create the same class just with:Then, you can just type
Person.new.name
and it will work exactly as your class.Checking that Person is an instance of class
Class
is as easy as typing in your replPerson.class
and you getClass
in return.Yes, Ruby classes are objects: