Ruby DSL’s vs YAML vs XML

I am using Ruby based DSL’s for a number of tasks in my current project. But one day it struck me – am I using the right tool for the job, could there be a better, more simple solution?

Some DSL’s I use are simply declarative and look something like this: quiz “Let’s ask stuff” do question “What is 1+1?” do alternative “2″, :correct => true alternative “3″ end question “Vad is 2+3″ do alternative “1″ alternative “Infinity” alternative “Don’t know” alternative “5″, :correct => true end end

I find that quite readable, but it requires me to maintain code that handle the DSL. So I figured, what if I used YAML? It is simple, and using it would mean less code to maintain.

After doing some experimenting, this is what I came up with: quiz: Let’s ask stuff questions: - question: What is 1+1 alternatives: - text: 2 correct: true – text: 3 - question: What is 2+3 alternatives: - text: 1 - text: “Infinity” - text: “Don’t know” - text: 5 correct: true

In my opinion, the YAML is not as readable as the Ruby DSL. I also find it more error prone as those dashes are sort of tricky.

XML? 2 3 1 Infinity Don’t know’ 5

With the correct schema, errors can be avoided, but it is just too much text in there, it gets heavy and verbose.

The verdict was to stay with the Ruby DSL’s – their readability and flexibility are well worth the effort of maintaining a separate parser – which is quite short really.

  • del.icio.us
  • Digg
  • DZone
  • Technorati
  • Reddit

Tags: , , , ,

  • Hi, it 's good content of DSL, in my country DSL on developing.
  • ...or Coco/Rb (better than Racc in my opinion):
    http://www.zenspider.com/ZSS/Products/CocoR/ (generates pure Ruby)
    http://raa.ruby-lang.org/cache/coco-rb/ (generates C extension to Ruby)
    , or even StringScanner:
    http://www.ruby-doc.org/core/classes/StringScan...
  • I've actually gone full circle and started doing external DSLs again. Check out Racc, the Yacc parser generator for Ruby.
  • That\'s JSON you mean, right? I do not think it is just that terse though.

    I believe it would be something like:

    {\"quiz\": {\"text\": \"Let\'s ask stuff\", \"alternatives\": {[\"text\"; 2, \"correct\": true ...

    It\'s better than XML, but I find it slightly less readable than YAML. Too many strings etc.

    JSON is really useful to send data over the wire, but I still prefer my DSL.

    Thanks for the input though.
  • Johan Lind
    What about the other YAML syntax:

    Let's ask stuff: { What is 1+1?: {2: correct, 3},
    Vad is 2+3?: {1, Infinity, Don't know, 5: correct}}
blog comments powered by Disqus