concept Ruby in category dsl

appears as: Ruby, Ruby
DSLs in Action

This is an excerpt from Manning's book DSLs in Action.

This code snippet creates a number of unit tests that can be run as the default task. Even if you don’t know Ruby, this snippet means the same thing to you; it’s just as expressive to you. How can that be? The snippet has explicit hotspots that match vocabulary you’re familiar with and provides an easy-to-use interface to the user of the DSL. In this case, Rake will be used by the developer. The language of the code uses semantics that match the level of abstraction that a developer expects and understands. Similarly, if you develop a DSL for the trader community, you need to keep in mind the level of expressiveness that suits the expectations and experiences of a trader at the dealing desk. This section contains a sidebar that has a short introduction to some of the basic terminology of the trading system. Have a look at the definitions because you’ll be using many of them in the example DSLs that you’ll develop over the course of the book.

Listing 4.6. Trade abstraction in Ruby

The languages I’ve selected for this chapter are dynamically typed. I start the chapter by discussing some of the attributes of dynamically typed languages that make good DSLs and why I selected Ruby, Groovy, and Clojure for our discussion. Then we’ll jump right into the implementation part and take you successively through the process of implementing complete DSLs using each of the three languages. We discuss the main features that these languages offer that you’ll frequently use in designing DSLs and some of the rationale for when to select which pattern for your implementation.

  • How are classes and objects defined in Ruby? Ruby is object-oriented (OO) and follows the usual notion of any other OO language to define a class. Ruby does have its own object model that has functionalities that let you change, inspect, and extend objects during runtime through metaprogramming.
  • How do you use the hash to implement a variable argument list? In Ruby, you can pass a hash as an argument to a method to emulate keyword arguments.