问题
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.
回答1:
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.
回答2:
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.
回答3:
Another example, of course, is Rake, the Ruby build system. What makes a DSL "good" in my opinion:
- 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.
- 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)
- 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.
回答4:
Have you checked out the Docile gem, it may be the easiest and cleanest way to meet your needs?
回答5:
Some good ruby DSLs I can think of are hpricot and sinatra
回答6:
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.
来源:https://stackoverflow.com/questions/899665/ruby-dsl-domain-specific-language-repositories-examples