public
Description: Some examples from my talk on beauty of Ruby's object model and power of modules/mixins/traits
Homepage: None!
Clone URL: git://github.com/michaelklishin/kiev_ruby_barcamp_2009.git
Sun Sep 27 08:24:07 -0700 2009
commit  e4618c2401d9197ac102e2e75217df30ba3a1016
tree    0816200ccfff1172f109e4b10e1ecd113200d4ab
parent  f83937670289e08a49faef074d909e33dd99c909
README.textile

What the heck is that?

These examples are from Ruby BarCamp ’09 in Kiev, Ukraine. I did not really prepare much, and
I had no slides at all, hence all the comments in this repository.

Points of the talk

  • Ruby object model is beautiful and rich. Learn it as good as you can.
  • Using rich object model and all the features built into it is way better
    compared to inventing your own OOP (e.g. alias_method_chain) or just
    not following three main ideas of OOP (incapsulation, inheritance, polymorphism)
  • Modules are the cornerstone of Ruby’s object model
  • Module inclusion results in an anonymous class being created in hosting class’ inheritance chain
  • Because of previous point, yon can call super from Module methods.
  • alias_method_chain was invented simply because some people did not know about this
    powerful feature of Ruby’s object model back in 2005
  • super works for methods of class objects (like Person.find or Person.all), too
  • Module + super does not solve all the library incompatibilities issues, but it helps
    a lot compared to alias_method_chain and other “screw incapsulation, I can do whatever I want”
    kind of inventions
  • class_eval replaces method implementation forcefully, and breaks superclass method calls
  • Safe way to do class_eval is to use Module.new, then class_eval on the module, and include it
  • And beware of local var vs. accessor with self issue when writing modules (and not just modules)
  • Rails 3 codebase is almost alias_method_chain free now. You bet, this means something.

Useful links

I personally learned a lot more about Ruby object model from Smalltalk books and articles than
from Ruby books and blog posts. A lot of Smalltalk books are in public domain or available for free.

Here is a few:

  • Ruby Hacking Guide translated from Japanese is invaluable, especially if you are fine with C