- learn about the conceptual framework Hodges' model. A tool that can help integrate HEALTH and SOCIAL CARE, INFORMATICS and EDUCATION. The model is situated, facilitates person-centredness, integrated - holistic care and reflective practice. A new site using Drupal is an ongoing aim - the creation of a reflective workbench. Email: h2cmng @ yahoo.co.uk Welcome

Saturday, February 23, 2008

Domain Specific Languages [II] + a Ruby mixin

Last year in one of those pick-it-up:put-it-down cycles, I surfed into this article on DSLs and Ruby c/o:

Artima logo

Ruby Code & Style Creating DSLs with Ruby
by Jim Freeze, March 16, 2006

Although as mentioned the other week, I'd read - and keep reading! - Chris Pine's intro Ruby book and the Thomas's Pickaxe, much of the code is hard to follow for a beginner - here's a snippet (from page 3 of 4):

[n.b. Some lines below are incomplete due to Blogger's interpretation of the characters]
Again, we update the require statement in dsl-loader.rb to load the mydsl4.rb file and run the loader:
% ruby dsl-loader.rb params.dsl
["@parameter", "@name"]
This is all well and good, but what if we don’t know the parameter names in advance? Depending on the use cases for the DSL, parameter names may be generated by the user. Never fear. With Ruby, we have the power of method_missing. A two-line method added to MyDSL will define a DSL attribute with dsl_accessor on demand. That is, if a value is to be assigned to a (thus far) non-existent parameter, method_missing will define the getters and setters and assign the value to the parameter.

% cat mydsl5.rb
require 'dslhelper'

class MyDSL
def method_missing(sym, *args)
self.class.dsl_accessor sym
send(sym, *args)

def self.load(filename)
# ... Same as before

% head -1 dsl-loader.rb
require 'mydsl5'

% ruby dsl-loader.rb params.dsl
["@parameter", "@name"]
Once again Olsen's - Design Patterns in Ruby has helped me (to begin to) understand  method-missing (p.184). I swear I heard some clanks as the pieces re-arranged themselves and one or two fell (almost) in place. Having copied Freeze's code into Ruby, it would not run. At least now the code is not as complex looking at it was and reading it through now it makes a lot more sense. I'm really looking forward to trying this again in Eclipse. On that first exposure I had a vague notion of the principle of method-missing, but reading Olsen's book I've a better grasp and may be able to keep hold of this and other Ruby concepts. Believe me that helps in my pick-it-up:put-it-down world.

more to follow ....