I am trying to understand the working of ruby open classes. In particular how does a method gets added to an existing class?
There must be some logic applied to identify if the class already exists.
Can someone please explain the concept.
Thanks in advance.
Class names are constants, so it’s fairly easy to see if a class name already exists:
So if a class is already defined (
Object.const_get('Array').is_a? Class) the methods get added to it, otherwise a new class gets defined. To be a bit more precise this happens on the C side of Ruby, but is more or less what it boils down to.