I have a piece of code (derived from
How do I create a class instance from a string name in ruby? - Stack Overflow),
which works like a charm, but I don’t fully understand WHY it works.
Here is a small example which describes the issue:
The last line prints “hello”.
On researching the details, I found the description of the method
Interestingly, const_get is not a method of Object, but of Module, and
indeed, changing the code to
works as well. I wanted to know however, why Object.const_get works too,
so I tried in irb:
Well, this explains why the above call works. Now to the puzzling part:
Class: Module (Ruby 1.8.6) mentions, that the parent
class of Module is Object. Indeed, a little experimenting shows:
This by itself is not surprising, because everything is an Object. But
why is an Object also a Module?