I was studying about Ruby's metaclass. I read this answer where it is nicely described what metaclass is. It's showed there when a class is created it will create two objects. Which is understandable. One for the class itself and one for it's metaclass. But when I am trying it myself I see that it is creating three objects.
puts "Before Class Creation object count - #{ObjectSpace.count_objects[:T_CLASS]}"
class Test
def self.foo # test_singleton
p 'Printed from method #foo'
end
def bar # test
p 'Printed from method #bar'
end
end
puts "After Class Creation object count - #{ObjectSpace.count_objects[:T_CLASS]}"
###############
Before Class Creation object count - 949
After Class Creation object count - 952
I am using Ruby - 2.5.1.
Can anyone help me understand this one?
Update:
The reference SO post that I added is using ruby-1.9.1 or greater, as the method count_objects for ObjectSpace was introduced in 1.9.1. It seems that the T_CLASS count has always always been 3 (tried with ruby-1.9.3-p551).
So, till now it's still a mystery why this answer. Ruby under a microscope also says the count is 2.