This post originated from an RSS feed registered with Ruby Buzz
by Rick DeNatale.
Original Post: RubyConf 2007 Friday Morning - Jim Weirich
Feed Title: Talk Like A Duck
Feed URL: http://talklikeaduck.denhaven2.com/articles.atom
Feed Description: Musings on Ruby, Rails, and other topics by an experienced object technologist.
Jim's talk was entitled "Advanced Ruby Class Design"
He started out talking about his own background with various programming languages and how influences from experience with other languages can both help and hamper learning Ruby. He said that while some things from Java/C++ like modularity carry over usefully to Ruby, but, that some very useful Ruby techniques are inconceivable to Java/C++ programmers.
He gave some examples of this from his "Classics", unfortunately, my note-taking skills are such that I can't capture enough to go into them in detail.
From Rake, an example of why the FileList class, which acts like an Array broke because it was initially implemented as a subclass of Array. The fix was to NOT make it a subclass of Array, something which can blow the mind of someone coming from a statically typed OO language.
From Builder, which uses "method_missing" to turn Ruby method calls into XML, a problem caused by interference between domain names, like 'class', and core Ruby object methods. The solution was to create a BlankSlate class which undefines most of it's inherited methods (it spares methods like __id__, and __send__). Then he found that he also needed to deal with methods added to Kernel and Object after BlankSlate was loaded. To do this he used method_added_hooks in Kernel and Object so that BlankSlate could remove newly added methods as they were defined. This later changed to use the append_features hook instead to catch methods added to Object or Kernel by module inclusion. Again, this is something which really requires coming from a less dynamic language to "Think outside the box."
He made another foray into "Parsing without Parsing" a Ruby technique for tracking the execution of a Ruby block with method_missing and proxy objects to "parse" the "source" of the block and capture an object graph representation. His motivating example was doing something like Err the Blog's Ambition which generates SQL queries from Ruby blocks. He gave lots of useful Ruby metaprogramming tips, up to and including where Ruby limitations led him to abandon this approach for SQL generation. Because of things like the inability to handle "syntactic sugar" operators like && and || since they aren't implemented as methods, he felt that these restrictions on contents of the block left him short of his goal to express the query in "natural" Ruby.
But the technique itself is quite useful, and finds use in tools like FlexMock and RSpec.
All in all a very thought provoking presentation from Jim. I hope he makes the slides available soon.