Instance variables for hidden classes?

● ARCHIVED · READ-ONLY
Started by Tsukihime 2 posts View original ↗
  1. r = Rect.new(0, 0, 100, 100)puts r.instance_variablesThis prints an empty array, but if you access the methods
    Code:
    xywidthheight
    They return the appropriate values.How is the data stored?

    Where is it stored?

    Why can't I see it?
  2. Instance variables are simply not used here. Not even hidden ones (name doesn't start with @, so they're inaccessible in Ruby, but not in C++). They are directly stored in dynamically allocated memory (a C++ POD struct in this case). The getter methods simply read the integer from the memory, convert it to a Fix- or Bignum, and return the converted value.

    If you want to know the details, you can lookup native extensions, Data (replaced by TypedData in newer Ruby versions) and T_DATA by reading README.EXT, which comes with the Ruby source, or this PDF (jukebox example), etc.