Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active October 13, 2017 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/90688b5457cbf13d624847d8304b3c87 to your computer and use it in GitHub Desktop.
Save JoshCheek/90688b5457cbf13d624847d8304b3c87 to your computer and use it in GitHub Desktop.
Object Model Notes for Gamut "platform basics"

Advance Object Model

Notes for Gamut "platform basics"

Structure

  • Objects
    • class
    • instance variables
  • Classes
    • superclass
    • instance methods
    • constants
    • ...object stuff (class/ivars)...
  • Bindings
    • self
    • local variables

Exercises

We did the ones on inheritance at https://gist.github.com/JoshCheek/ad9f70a6d855be9ed50d

New idea: Singleton Class

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"

A few questions to contemplate

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?

If you want more

If you to do more of these

Here's some other things we could talk about later, just let me know:

  • The callstack
  • How modules work?
  • Write a minimal Ruby interpreter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment