15

I am seeking excellent examples of Ruby DSLs (Domain Specific Languages). Which repositories, projects do you know of that are worth a read? Why is it (or: are they) great examples?

I am particularly interested in more complex examples that are well thought-out and designed.

Demi
  • 6,147
  • 7
  • 36
  • 38

6 Answers6

12

Rake and Rack are some good examples of DSL's. If you want some more examples, check these out:

  • Sinatra is a very popular DSL for building web applications, and it's open source on GitHub.
  • Twibot is a newer DSL inspired by Sinatra that lets you create Twitter bots that automatically respond to messages and replies.

If you want to get started on making your own, here's an excellent tutorial called Building a DSL in Ruby.

Justin Poliey
  • 16,289
  • 7
  • 37
  • 48
3

In the area of Behaviour-Driven Development you can check out:

  • Cucumber - Describe BDD using scenarios
  • RSpec - Replace testing code with specifying behaviours.

Though I have to admit the RSpec code leaves me scratching my head sometimes because I'm still very much a novice.

Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
2

Have you checked out the Docile gem, it may be the easiest and cleanest way to meet your needs?

ms-tg
  • 2,688
  • 23
  • 18
2

Another example, of course, is Rake, the Ruby build system. What makes a DSL "good" in my opinion:

  1. Notation conforms to meaning, i.e. if you read a sentence (statement) in the DSL, you have a clear, unambiguous idea of what it does.
  2. Domain-specific, i.e. the DSL does not solve every problem in the universe but rather focuses on one little domain (such as building software, querying data, or contructing UIs)
  3. High-level of abstraction. A DSL uses high-level concepts that the programmer can use, and translates those to a lower-level implementation (internally). In the case of Rake the main concept the language is based on are tasks and dependencies between them.
Zef Hemel
  • 6,457
  • 3
  • 20
  • 14
1

Some good ruby DSLs I can think of are hpricot and sinatra

Craig
  • 7,471
  • 4
  • 29
  • 46
  • The structure of sinatra is really nice. Had heard about it but not looked at it. Thanks for the reminder. – Demi May 22 '09 at 20:31
0

Ruby on Rails' Active Record is a DSL!

http://apidock.com/rails/ActiveRecord/Base

Also this episode of code school might be interesting as it leads up to building a DSL (where I learned Active Record was a DSL)

http://www.codeschool.com/courses/ruby-bits-part-2

The above course also talks about external and internal DSLs. Cucumber is an example of an external DSL where you need to build a parser and compiler, etc. Active Record is an example of an Internal DSL that runs inside some existing code.

Rimian
  • 36,864
  • 16
  • 117
  • 117