Friday, July 24, 2009

Great Talk on the Ruby Object Model

Dave Thomas speaks at Scotland on Rails 2009. Thanks much to Richard for forwarding this:
* http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4

Intro by mentioning that the progenitors of Ruby's object-orientedness are first from Simula language, then Smalltalk and Lisp. Mentioned that should check out Simula and Smalltalk. He said that Squeak is great for Smalltalk and to read Kent Beck's Smalltalk Best Practice Patterns book.

A lot of talk about "self" in Ruby, the Singleton (Virtual) object, and more. Dave is definitely not a fan of languages that use "classes" or "inheritance".

Understanding self:
* with a method is called, self changes to the receiver
* looks up method name in self's class. If can't find in self, then check parent, then check its parent all the way up to Object (or beyond that to BasicObject in Ruby 1.9). If still not found, will do method_missing method call in self's class, and its parent, etc. all the way up

Singleton class:
* aka Eigenclass, Metaclass, Ghost Class, Virtual Class
* Normal class but hidden (has a special flag)
* Only one per object

Golden Rule:
* Instance variables: look up in self
* Methods: look up in self's class

include is NOT including the code of the (module) but rather putting methods, etc. into the singleton class(but it was fuzzy about what happens with multiple includes- they get stacked or some such)

Miscellaneous:
* inspect does something different in 1.9.
* < is not overrideable (is syntax)

All you have to do to get metaprogramming is:
* understand how self works
* understand how self is changed by a either by a method call or by a class definition
* understand that instance variables are always stored in self and referenced in self
* all method calls in ruby are identical
* there are no special cases
* every method call is right and then up. (if your inside a class object- right and then up)

No comments: