Notes for Gamut "platform basics"
- Objects
- class
- instance variables
- Classes
- superclass
- instance methods
- constants
- ...object stuff (class/ivars)...
- Bindings
- self
- local variables
We did the ones on inheritance at https://gist.github.com/JoshCheek/ad9f70a6d855be9ed50d
We can use this model to add methods to one individual object. We would create a new class for it, this is called a "singleton class", and then define its methods in there. The singleton class's superclass would be the original class, which would allow it to continue to behave as normal in all other situations.
This is what "class methods" are.
This is what allows main to have a different inspection, even though it is just an object. Here is the definition of main, in the C code, it is approximately this Ruby code:
my_main = Object.new
my_main.inspect # => "#<Object:0x007ff1ee0edac0>"
def my_main.inspect() "main" end
my_main.inspect # => "main"
Which is syntactic sugar for this Ruby code:
my_main = Object.new
my_main.inspect # => "#<Object:0x007f9c000ad680>"
my_main.singleton_class.class_eval { def inspect() "main" end }
my_main.inspect # => "main"
The purpose of these is to get you to relate the different structures. There is a reason that it takes the structure it does, and you can contemplate it to understand why, and what the implications of it are:
- What's the point of
self
? - How are the three structures similar?
- If we were implementing these in a database, what would be objects, classes, and instance variables be?
- Exercises:
- The method call algorithm:
- The material here, but written out:
Here's some other things we could talk about later, just let me know:
- The callstack
- How modules work?
- Write a minimal Ruby interpreter